Page 1 of 1

mysql syntax error... I don't understand

Posted: Tue Apr 26, 2011 3:05 am
by danwguy
I can't see any error in this script and it's driving me insane. Would someone please (I'm begging) please take a look and see where the error is. It makes no sense to me and I'm too bald already to keep pulling my hair out with this error. Thank you in advance...

Code: Select all

$sql = mysql_query("INSERT INTO courses (location, course, instructor, blurb, description) VALUES ('$location', '$class', '$teacher', '$blurb', '$description'");
		if(!$sql) {
			echo "Sorry there was a problem adding that class due to the following error:";
			echo "<br />- " .mysql_error();
                                                echo "<br />" .$teacher;
                                                echo "<br />" .$class;
                                                echo "<br />" .$location;
                                                echo "<br />" .$blurb;
                                                echo "<br />" .$description;
			exit();
			}
The above script ouputs the following error...
Sorry there was a problem adding that class due to the following 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 '' at line 1
Mary Mezinis
Amazing English
classroom
this is just a test course to see if the add script is working.
Please do not try to sign up for this class, it is just a test to see if the add script is working properly.
please please please help me on this one, I am losing my mind as I can't see the error and the mysql_error() isn't helpfull at all. Thank you again,

Re: mysql syntax error... I don't understand

Posted: Tue Apr 26, 2011 3:47 am
by andym01480
If you change the $sql to just the statement, you can then echo it to see what is wrong, which you will see is that you not put the closing bracket on the VALUES after the last value!

Code: Select all

$sql = "INSERT INTO courses (location, course, instructor, blurb, description) VALUES ('$location', '$class', '$teacher', '$blurb', '$description' )";
echo $sql;//remove once sorted
$result=mysql_query($sql);
                if(!$result) {
                        echo "Sorry there was a problem adding that class due to the following error:";
                        echo "<br />- " .mysql_error();
                                                echo "<br />" .$teacher;
                                                echo "<br />" .$class;
                                                echo "<br />" .$location;
                                                echo "<br />" .$blurb;
                                                echo "<br />" .$description;
                        exit();
                        }
 
You'll need to make sure you ahve made the user input is escaped too - mysql_real_escape_string() is good for that!

Re: mysql syntax error... I don't understand

Posted: Tue Apr 26, 2011 9:02 am
by prensil
The above solution would work definitely. But you should always use the MySQL wrapper classes to run the queries.

Re: mysql syntax error... I don't understand

Posted: Tue Apr 26, 2011 3:29 pm
by danwguy
Thank you, I can't believe I didn't see that missing ) in there.

Re: mysql syntax error... I don't understand

Posted: Tue Apr 26, 2011 6:34 pm
by califdon
danwguy wrote:Thank you, I can't believe I didn't see that missing ) in there.
That's the point andym01480 was making. You should always assign your SQL statement to a string variable (by convention, $sql) and help yourself find these oversight errors. You can concatenate the variable with the mysql_error() output so that if there IS an error, it will echo out both the error message and the SQL syntax.