2

Alright I don't know why this line code isn't working. I have used it on a site before so now I am coping and pasting and I get an error on this site but didn't get one on the last.

   $query = "INSERT INTO calendar (group, date, subject, info,) 
   VALUES ('$group', '$date', '$subject', '$info')";

^^^^ My query

   if(mysql_query($query)){
         echo "Form Successfully Submited!";
   }

^^^^ Submitting the query

Like I have said I have used this code before with no error but now I get a error. The error says Query Submit Failed: 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 'group, date, subject, info,) VALUES ('SlowTheTurtle', '0412014', 'asdfasdf', 'a' at line 1

3
  • 2
    Get rid of the comma after info Commented Apr 20, 2014 at 2:35
  • it seems that you have additional comma on selecting fields, also it's not recommend to use the deprecated mysql_* functions. Commented Apr 20, 2014 at 2:36
  • Is there possible an apostrophe in the $info variable? If so, not only will it crash this query, but your code is open to SQL injection. Note also that mysql_*() is deprecated and should not be used for new code. Use mysqli_*() or PDO instead. Commented Apr 20, 2014 at 2:36

2 Answers 2

1

group is a reserved word(s) in MySQL. You need to wrap them using backticks.

Must be like this..

$query = "INSERT INTO calendar (`group`, `date`, `subject`, `info`) 
   VALUES ('$group', '$date', '$subject', '$info')";

This (mysql_*) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, Prepared Statements of MySQLi or PDO_MySQL extension should be used to ward off SQL Injection attacks !

Sign up to request clarification or add additional context in comments.

Comments

0

There is a comma after the first word 'info', not following by another column name. Could that be it?

Try:

$query = "INSERT INTO calendar (group, date, subject, info)
    VALUES ('$group', '$date', '$subject', '$info')";

3 Comments

good eye on the extra comma, but the bigger issue is the group as it is a reserved word, as in GROUP BY
Thanks @Sean. Good to know! I guess I've never had to populate a table with a column named 'group' in a MySQL DB before.
Ricardo, @Sean is right 100% , as you can see the error states .. right syntax to use near 'group, d , so that gives you a hint :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.