views:

48

answers:

2

i have a messaging system and it works fine but i have it so when its read it mysql_querys and sets read to 1. so that way in futer you can tell if its opend. it does not update here is the script for viewing the message where its suppose to update. THANKS

<?php
session_start();

require "../scripts/connect_to_mysql.php";

    if (isset($_SESSION['id'])){
    $touser = $_SESSION['id'];
        }
        elseif (!isset($_SESSION['id'])){
            header('location: http://www.stat-me.com');
        }

$id = $_GET['id'];
$memberfirstname = $_SESSION['firstname'];


if(!isset($id)) {
    header('location: inbox.php');
}
elseif(isset($id)) {

    mysql_query("UPDATE pms SET read='1' WHERE id='$id'");    


    $grab_pm = mysql_query("SELECT * FROM pms WHERE touser = '$touser' AND id = '$id'");

    while($r= mysql_fetch_object($grab_pm)) {    
        $subject = $r->subject;    
        $message = $r->message;    
        $fromuser = $r->fromuser;    
        $datesent = $r->datesent;    
        $read = $r->read;
    }


}


?>
A: 

It's not entirely clear if the id field is an INT but I'm guessing so, in which case fix the code as follows (remove the single quotes around $id):

mysql_query("UPDATE pms SET read='1' WHERE id=$id");
$grab_pm = mysql_query("SELECT * FROM pms WHERE touser = $touser AND id = $id");

Also be sure to escape your GET variables, e.g.

$id = mysql_real_escape_string($_GET['id']) ;

EDIT: also take single quotes around $touser above

Dan U.
still nothing. i dont understand why its not working
Matthew Carter
and id is an INTand read is an ENUM
Matthew Carter
Ah, I bet touser is an INT too, right? I'll edit the answer to reflect that. Also, have you debugged it? on a test page or development site, echo out your queries to the browser and copy and paste them directly into your database, e.g. using phpMyAdmin -- do you get an error?
Dan U.
touser is an int but when it gets to that query it works the problem is the line obove its not setting read to 1. and im kinda new to this what do u mean echo the querys.thanks
Matthew Carter
add to your code: echo "UPDATE pms SET read='1' WHERE id=$id"; run the page and you'll see the query on the page, then copy and paste that into your database. Another thought, is to do SET read=1 -- that might work, though I don't know.
Dan U.
i copyed UPDATE pms SET read='1' WHERE id=18 and put that in and it said:ErrorSQL query:UPDATE pms SET READ = '1' WHERE id =18MySQL said: Documentation#1064 - 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='1' WHERE id=18' at line 1
Matthew Carter
double check the database table definition for read -- what are the ENUM options that are specified? The value you pass to read needs to match one of those.
Dan U.
this is what is under read :enum('0', '1') utf8_general_cithis is so weird
Matthew Carter
hmm, you might try changing the field name -- "read" is a reserved word per http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
Dan U.
okay some progress. it only worked this time when i put it in manually. i dont know why here is the updated code.:elseif(isset($id)) {mysql_query("UPDATE pms SET opened=1 WHERE id=$id"); mysql_query("SET opened='1' WHERE id=$id"); echo "UPDATE pms SET opened='1' WHERE id=$id";$grab_pm = mysql_query("SELECT * FROM pms WHERE touser = $touser AND id = $id");while($r= mysql_fetch_object($grab_pm)) {$subject = $r->subject;$message = $r->message;$fromuser = $r->fromuser;$datesent = $r->datesent;$read = $r->read;
Matthew Carter
I LOVE YOU. I GOT IT TO work just missing quotes. you were right about the reserved word never thought of that thank you so much i cant belive you sticked with me all this time. your a hero. and thank i never thught of escaping url. i didnt know you could get attacked like that
Matthew Carter
no problem ... btw if this helped you can accept the answer by checking the checkmark. Good luck with your work!
Dan U.
A: 

Change your queries to

mysql_query("UPDATE pms SET read='1' WHERE id=".$id);
$grab_pm = mysql_query("SELECT * FROM pms WHERE touser = ".$touser." AND id = ".$id);

INT datatypes come without double quotes, enums depend on their content (so if you inserted '1' and '0' in example, delete the quotes around '1', if otherwise, keep them).

cypher