SQL Syntax error in insert query

Hi to all i am getting error in insert query below is query

echo $sql="INSERT INTO event(mode,title,desc,date,status)VALUES('$mode','$title','$desc','$date','$status')";

and below is error

Error in your SQL syntax

Check the manual that corresponds to your MySQL server version for the right syntax to use near'desc,date,status)VALUES('Event','fdsagsd','fa sdgavsd ','2015-01-21','1')' at line 1

Jon Skeet
people
quotationmark

The problem is that desc is a reserved word (think of order by), so you need to quota it with backticks when you're using it as a column name.

From section 9.2 of the MySQL documentation:

Certain objects within MySQL, including database, table, index, column, alias, view, stored procedure, partition, tablespace, and other object names are known as identifiers. This section describes the permissible syntax for identifiers in MySQL. Section 9.2.2, “Identifier Case Sensitivity”, describes which types of identifiers are case sensitive and under what conditions.

An identifier may be quoted or unquoted. If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it.

...

The identifier quote character is the backtick (“`”):

mysql> SELECT * FROM `select` WHERE `select`.id > 100;

So the SQL you want is:

INSERT INTO event(mode, title, `desc`, date, status)
VALUES ('Event', 'fdsagsd', 'fa sdgavsd ', '2015-01-21','1')

(The extra spaces aren't necessary of course, but I'd recommend including them just to make the SQL more readable.)

Of course, given that you'll need to quote the column name everywhere you use it, you may well decide it would be simpler to just rename it so that it's not a reserved word - that partly depends on how much other code already uses the schema.

people

See more on this question at Stackoverflow