Page 1 of 1

Syntax Error, But can't find it.

Posted: Sun Oct 14, 2012 1:19 am
by OoZI
Here is my Code:

Code: Select all

  
<?php
session_start();
include ('header.php');

$db = mysql_connect("mysql2.000webhost.com","a7118719_agents","********","a7118719_agents") or die("Could Not Connect");
if(!$db)
die("No Database");
if(!mysql_select_db("a7118719_agents",$db))
die("No Database Selected");

$to = $_POST['to'];
$pri = $_POST['pri'];
$message = $_POST['message'];
$from = $_SESSION['name'];

$mQ = "INSERT INTO messages (to, from, message, priority) VALUES ('$to','$from','$message','$pri')";

$mS = mysql_query($mQ) or die ("ERROR ID: 1");

echo "Message Sent!";

?>





It continues returning Error 1, so it can't do the Query... Why not?

Re: Syntax Error, But can't find it.

Posted: Sun Oct 14, 2012 1:57 am
by Torniquet
do

Code: Select all

$mS = mysql_query($mQ) or die (mysql_error()); 
it should give you the reason why your query is failing.

Re: Syntax Error, But can't find it.

Posted: Sun Oct 14, 2012 4:02 pm
by OoZI
So I did that, and I got this.

Code: Select all

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, message, priority) VALUES ('David Faulkner','Kyle Dawson','This is a T' at line 1
I still can't find the error...

Re: Syntax Error, But can't find it.

Posted: Sun Oct 14, 2012 7:56 pm
by Jackolantern
You have used 2 different MySQL reserved words in your query: "to" and "from". You cannot use these as names of anything in your database unless you put `back-ticks` around them in the query, but even then it is highly suggested you do not use them. Keeping them sets you up for headaches, since every query will fail that contains those names when not back-ticked.

I don't know why MySQL doesn't give better error messages when you use reserve words, but it probably has to do with performance gains by not parsing the query before execution. You can see in your error message the query broke when it hit "to", so when you see that, or you just can't debug a query, always check the rather extensive list of MySQL reserve words I linked above to make sure you aren't using any of them :)

Re: Syntax Error, But can't find it.

Posted: Sun Oct 14, 2012 9:22 pm
by OoZI
*facepalm* I would have never thought of that... Thanks.

Re: Syntax Error, But can't find it.

Posted: Mon Oct 15, 2012 12:47 am
by Jackolantern
OoZI wrote:*facepalm* I would have never thought of that... Thanks.
Most people wouldn't because of the poor error messages for reserve word violations if they hadn't seen one before. Don't feel bad :)