cmattoon Posted October 11, 2011 Share Posted October 11, 2011 I'm using a PM script from here: http://www.pixel2life.com/publish/tutorials/608/simple_private_messaging_system/ and can't get the messages to send. This is the relevant section of code (from the PHP class), along with the mysql_error() output. function sendmessage($to,$title,$message) { $to = $this->getuserid($to); //$sql = "INSERT INTO messages SET 'to' = '".$to."', 'from' = '".$this->userid."', 'title' = '".$title."', 'message' = '".trim($message)."', 'created' = NOW()"; $created = time(); $sql = "INSERT INTO messages ('to,from,subject,message,created') VALUES ('$to','$this->userid','$title','".trim($message)."','$created')"; return (@mysql_query($sql)) ? true : false; } 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 ''to,from,subject,message,created') VALUES ('5','1','test','This is a test Messag' at line 1 I have tried the original code (commented out), as well as the subsequent two lines with several variations in quotes. Each one results in this message. The trim() on the second one was also attempted with just '$message', and did not work. The values in the INSERT statement are correct, as far as I can tell. The only potential difference is the integers are actually strings, being put into an INT(11) field on the DB...but I've never had a problem with this before. I've also tried re-ordering the query to match the order of fields in the database (title,message,to,from,created). $sql = "INSERT INTO messages (title,message,to,from,created) VALUES ('$title','".trim($message)."','$to','$this->userid','$created')"; This results in the error showing: 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 'to,from,created) VALUES ('test','This is a test Message','5','1','1318345125')' at line 1 It appears to be a problem with the "to"? I've never had this problem before...am I missing something? Quote Link to comment https://forums.phpfreaks.com/topic/248901-mysql-syntax-error/ Share on other sites More sharing options...
Pikachu2000 Posted October 11, 2011 Share Posted October 11, 2011 MySQL database, table and field names don't get quoted. If the name happens to be a MySQL reserved word, you would use `backticks` to enclose it. Quote Link to comment https://forums.phpfreaks.com/topic/248901-mysql-syntax-error/#findComment-1278273 Share on other sites More sharing options...
cmattoon Posted October 11, 2011 Author Share Posted October 11, 2011 Ahhhhhhhh. That's strange, I believe I've used "to" as a field before with no problems.. And yeah, those quotes were just me trying different arrangements out of frustration. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/248901-mysql-syntax-error/#findComment-1278278 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.