简体   繁体   English

为什么此MySQL语句语法错误?

[英]Why is this MySQL statement syntax wrong?

I am using prepared statements to process incoming post data, clean the array, and insert into a MySQL database. 我正在使用准备好的语句来处理传入的帖子数据,清理数组并插入到MySQL数据库中。

Here is the SQL statement just before it is submitted: 这是提交前的SQL语句:

INSERT INTO LoggedCarts (Bill-Address1,Bill-Address2,Bill-City,Bill-Company,Bill-Country,Bill-Email,Bill-Firstname,Bill-Lastname,Bill-Name,Bill-Phone,Bill-State,Bill-Zip,Card-Expiry,Card-Name,Card-Number,Comments,Date,ID,IP,Item-Code-1,Item-Count,Item-Description-1,Item-Id-1,Item-Quantity-1,Item-Taxable-1,Item-Thumb-1,Item-Unit-Price-1,Item-Url-1,Numeric-Time,Ship-Address1,Ship-Address2,Ship-City,Ship-Company,Ship-Country,Ship-Email,Ship-Firstname,Ship-Lastname,Ship-Name,Ship-Phone,Ship-State,Ship-Zip,Shipping,Space-Id,Store-Id,Store-Name,Tax-Charge,Total) VALUES ("Pineapple Highway","","Orange","","US United States","casedilla@hotmail.com","Bob","Dole","Bob Dole","9075554509","CA","97056","","Check","NumberTemporarilyUnavailable","","Tue Dec 10 16:55:11 2013 GMT","yhst-130408242826480-485","50.78.241.193","TERRALUX-TT-5","1","Terralux TT-5 LED Tactical Flashlight 650 Lumens Uses 2 x CR123 or 1 x 18650","terralux-tt-5","3","YES","","112.49","http://www.batteryjunction.com/terralux-tt-5.html","1386694511","Pineapple Highway","","Orange","","US United States","casedilla@hotmail.com","Bob","Dole","Bob Dole","9075554509","CA","97056","Air (3-5 days)","","yhst-130408242826480","BatteryJunction.com","0.00","337.47")

Question is, what is wrong with the syntax? 问题是,语法有什么问题? The same code is also used for a different table that holds abandoned carts and it writes fine. 相同的代码还用于容纳废弃推车的其他表,并且写得很好。

Note: As has been pointed out below, the use of hyphens in column names is frowned on, as it requires special preparation prior to submitting the query. 注意:正如下面所指出的,在列名中连字符的使用被禁止了,因为在提交查询之前需要特殊的准备。 In this instance, I am matching the column names to the incoming post data key array. 在这种情况下,我将列名与传入的发布数据键数组匹配。 I could have gone through and cleaned the key array, removing the hyphens, which would have been an alternative solution. 我本可以检查并清理键数组,删除连字符,这将是替代解决方案。

All in all, as suggested below, the correct solution to the question of why this SQL statement is failing is the use of special characters(hyphen) in the column name, resulting in the required use of a backtick around the column name(backtick = ` where as apostrophe = ') allowing the column name to be read unbroken. 总而言之,如下所述,对于此SQL语句为什么失败的问题,正确的解决方案是在列名中使用特殊字符(连字符),从而导致在列名周围使用了反引号(backtick = `where as撇号=')允许连续读取列名。

Observing where the break occurred by testing the original statement in PHPMyAdmin: 通过在PHPMyAdmin中测试原始语句来观察中断发生的位置:

#1064 - 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 '-Address1,Bill-Address2,Bill-City,Bill-Company,Bill-Country,Bill-Email,Bill-Firs' at line 1

did give a clue as to why the statement was failing. 确实提供了有关声明失败的线索。

Thanks for the help guys! 感谢您的帮助!

You need to escape column names with special characters with backticks. 您需要使用带有反引号的特殊字符对列名进行转义。 - is a special character. -是一个特殊字符。 Use 采用

INSERT INTO LoggedCarts (`Bill-Address1`, ...

如果这些字段名称确实包含减号,则需要将其括在反引号中:

`Bill-Address1`

尝试使用单引号插入字符串值。

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

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