CoderGenius Posted April 26, 2008 Share Posted April 26, 2008 I've been working on a new online game for a few weeks now, an am in the middle of trying to set up a simple messaging system. Right now I'm using a simple Table with columns for id, to, from, subject, message, timesent, and read. my problem is that when i try to insert new messages into the table, they simply never are inserted! I've been checking my syntax for a few days now, an I'm baffled as to why this statement wont work... Here is the portion of the code that isn't working: if($ready == "Send"){//SEND WAS CLICKED $check=mysql_query("SELECT * FROM players WHERE id='$to'"); $rowcheck=mysql_fetch_array($check); if($rowcheck['user'] == ""){ ?> <script language="JavaScript"> <!-- alert("That player does not exist."); //--> </script> <?php }else{ mysql_query("INSERT INTO messages (to,from,subject,message) VALUES($to,$from,$subject,$message);"); goTown("Message Sent!"); } }//IF SEND WAS CLICKED The problem is in the insert statement i believe. I've tested the query_fetch_array, and it is in fact returning the correct user that has been specified in the 'to' field. and the function 'goTown' is working, as it is sending the alert that the message was successfully sent. Any help would be greatly appreciated! PS, if you wanna see the code in action, the game I'm working on is actually online, an playable (except the messaging of course ) heres the URL right now: http://www.videogamenewz.com/LR Quote Link to comment Share on other sites More sharing options...
mwasif Posted April 26, 2008 Share Posted April 26, 2008 You need to enlcose string values in single quotes. mysql_query("INSERT INTO messages (to,from,subject,message) VALUES('$to','$from','$subject','$message');"); Quote Link to comment Share on other sites More sharing options...
CoderGenius Posted April 26, 2008 Author Share Posted April 26, 2008 oops... i was trying something out earlier an accidentally forgot to put the quotes back in... but it still doesn't work with them added... heres how the code should actually look: if($ready == "Send"){//SEND WAS CLICKED $check=mysql_query("SELECT * FROM players WHERE id='$to'"); $rowcheck=mysql_fetch_array($check); if($rowcheck['user'] == ""){ ?> <script language="JavaScript"> <!-- alert("That player does not exist."); //--> </script> <?php }else{ mysql_query("INSERT INTO messages (to,from,subject,message) VALUES('$to','$from','$subject','$message')"); goTown("Message Sent!"); } Quote Link to comment Share on other sites More sharing options...
CoderGenius Posted April 26, 2008 Author Share Posted April 26, 2008 oh an i forgot to mention before, tho i doubt it has anything to do with my problem (its just one of the guidline thingys) im using MySQL version 5.0 Quote Link to comment Share on other sites More sharing options...
mwasif Posted April 26, 2008 Share Posted April 26, 2008 Make sure you are connected to the right database. Use mysql_error() to view the errors. mysql_query("INSERT INTO messages (to,from,subject,message) VALUES('$to','$from','$subject','$message')") or die ("Error: ".mysql_error()); Can you see any error now? Quote Link to comment Share on other sites More sharing options...
CoderGenius Posted April 26, 2008 Author Share Posted April 26, 2008 wow, i cant believe i forgot to put that in there... ok here's the error its generating: Error: 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) VALUES('1','Quiet Master','TEST','A TEST FOR THE SYSTEM' at line 1 Quote Link to comment Share on other sites More sharing options...
mwasif Posted April 26, 2008 Share Posted April 26, 2008 You need to use backticks around to and from as these are the reserve words mysql_query("INSERT INTO messages (`to`,`from`,subject,message) VALUES('$to','$from','$subject','$message')") or die ("Error: ".mysql_error()); Quote Link to comment Share on other sites More sharing options...
CoderGenius Posted April 26, 2008 Author Share Posted April 26, 2008 i would never have thought of that... Thanks so much! it works perfectly now!!! ill prolly go in an change those column names now, so i wont accidentally forget those ticks later... Quote Link to comment Share on other sites More sharing options...
mwasif Posted April 26, 2008 Share Posted April 26, 2008 ill prolly go in an change those column names now, so i wont accidentally forget those ticks later... Thats a better solution. Quote Link to comment 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.