Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    91
    Thanks
    21
    Thanked 0 Times in 0 Posts

    Update Private Messages Table

    Hi,

    I'm a newbie when it comes to PHP code, so please bear with me. I'm getting nowhere with my codes. I've coded a private message system for my site, but the problem is, it doesn't update the MySQL database with "set read = 1 where id =bla". When someone clicks the "read message" button, it'll go to a different page, with this code on top:

    Code:
    <?
    $query = "UPDATE private_messages SET read = 1 WHERE id = '".$_GET['id']."'";
    ?>
    I simply wish to set the '0' to '1', so I can use that as 'This message has been read' and to get the total new messages from the database.

    I've also tried another code:

    Code:
    <?
    $id=$_GET['id'];
    
    $result = mysql_query("SELECT * FROM private_messages WHERE id = '$id' LIMIT 1") or die(mysql_error());
    
    while($row = mysql_fetch_array($result)){
    
    $update = mysql_query("UPDATE private_messages SET read = 1 WHERE id = '$id' LIMIT 1") or die(mysql_error());
    
    }
    ?>
    The above code for my downloads, when I wish to add a '1' to the total count. But it doesn't set the 'read' part to 1.

    That's that. It simply doesn't work. As for the other thing I cannot get:

    Code:
    $count = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM private_messages WHERE read = 0 AND userid='".$_SESSION['user_id']."'"),0);
    It works when I leave the 'WHERE read = 0' part out. It also doesn't work when I try WHERE 'bla' = 'bla' AND WHERE 'bla' = 'bla'. It's kinda confusing, since I cannot find the solution to my problem anywhere.

    Many thanks for any help in advance...

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    For your first one try echo'ing the query before you execute it to make sure it's properly formatted, and use "or die(mysql_error());" like you did in the other query.
    For the last one use this code to find out the problem(or echo the query):
    PHP Code:
    $result mysql_query("SELECT COUNT(*) FROM private_messages WHERE read = 0 AND userid='".$_SESSION['user_id']."'") or die(mysql_error());
    $count mysql_result($result0) or die(mysql_error()); 

  • #3
    New Coder
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    91
    Thanks
    21
    Thanked 0 Times in 0 Posts
    Thanks,

    I've just tested the code you posted and I'm ending up with this:

    Code:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read = 0 AND userid='1'' at line 1
    I've also had that before, weird considering it should indeed work.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Try this:
    PHP Code:
    $count mysql_result(mysql_query("SELECT COUNT(*) FROM `private_messages` WHERE `read` = '0' AND `userid` = '".$_SESSION['user_id']."'"),0); 

  • #5
    New Coder
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    91
    Thanks
    21
    Thanked 0 Times in 0 Posts
    Thanks,

    That helps. Not sure why though, since the other one should have worked also. Ah, whatever works is good enough for me

    As for the update stuff, I'll try some more things before I post here again. Maybe I could figure it out myself, I'm in a good state right now and everything seems to work out...

    Thanks again for the help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •