简体   繁体   English

我的SQL / PHP语法在这里有什么问题?

[英]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. 我主要使用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. 设置页面调度的方式消除了表单发布的可能性,因此,我让JS序列化数据并重新加载页面,然后在PHP中反序列化数据,将值粘贴到mysql查询中,然后尝试运行它。

Here's what my SQL code looks like inside of PHP: 这是我的SQL代码在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; 您的SQL语法有误; 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 检查与您的MySQL服务器版本相对应的手册,以在第1行的'desc)VALUES('a56','test.zip','cake')'附近使用正确的语法

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. 而且奇怪的是,运行查询的代码行不在第1行,而在第28行。

Any help is appreciated :) 任何帮助表示赞赏:)

desc is a reserved keyword in MySQL. desc是MySQL中的保留关键字

The recommended workaround is to use backticks . 推荐的解决方法是使用反引号 From MySQL manual : MySQL手册

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 也看一下这个问题: 何时在MySQL中使用单引号,双引号和反引号

desc is a reserved word in SQL. desc是SQL中的保留字。

It is (together with ASC ) used to determine the sorting order of the results. 它(与ASC一起)用于确定结果的排序顺序。

Please change the column name of desc, its reserved by MYSQL. 请更改desc的列名,该列名由MYSQL保留。 for more details please see list of reserved words on MYSQL Reserved Words 有关更多详细信息,请参见MYSQL保留字上的保留字列表。

http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html 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. 希望这对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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