简体   繁体   中英

What is wrong with my SQL/PHP syntax here?

So I have a database of downloads on my site set up that enables me to track the number of downloads, and I'm trying to set up a front-end for me and my compatriots to insert new downloads into the database. I'm setting up the front-end with primarily PHP.

The way my paging is set up removes the possibility of my forms simply posting, so instead I have JS serializing the data and reloading the page, then I unserialize the data in PHP, stick the values into a mysql query, and try to run it.

Here's what my SQL code looks like inside of PHP:

$sql  = "INSERT INTO dl (id, file, desc) VALUES ('$idd', '$file', '$desc')";

Which turns into this string:

INSERT INTO dl (id, file, desc) VALUES ('a56', 'test.zip', 'cake')

But when the page tries to run it, I get this 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 'desc) VALUES ('a56', 'test.zip', 'cake')' at line 1

And the weirdness of that is compounded by the fact that the line of code running the query is not on line 1. It's on line 28.

Any help is appreciated :)

desc is a reserved keyword in MySQL.

The recommended workaround is to use backticks . From MySQL manual :

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

If renaming the table or column isn't possible, wrap the offending identifier in backticks (`):

$sql  = "INSERT INTO dl (id, `file`, `desc`) VALUES ('$idd', '$file', '$desc')";

Take a look at this question too: When to use single quotes, double quotes, and backticks in MySQL

desc is a reserved word in SQL.

It is (together with ASC ) used to determine the sorting order of the results.

Please change the column name of desc, its reserved by MYSQL. for more details please see list of reserved words on MYSQL Reserved Words

http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Please try this.

$sql  = "INSERT INTO dl (`id`, `file`, `desc`) VALUES ('".$idd."', '".$file."', '".$desc."')";

hope this is your useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM