mysql syntax error... I don't understand

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

mysql syntax error... I don't understand

Post 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,
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

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

Post 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!
prensil
Forum Newbie
Posts: 15
Joined: Tue Apr 26, 2011 8:38 am
Location: Ahmedabad
Contact:

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

Post by prensil »

The above solution would work definitely. But you should always use the MySQL wrapper classes to run the queries.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

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

Post by danwguy »

Thank you, I can't believe I didn't see that missing ) in there.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
Post Reply