我是新来的,所以如果我做错了什么,或者应该做一些我不知道的事情,就告诉我!
我的问题是:我正在通过PHP运行一个sql查询,一切都很好。这是以下查询:

$query = "INSERT INTO products ( name, type, brand, price, discount, dateAdded, categories, status, description, code, weight, insurance, frame, fork, transmission, brakes, isDiscount, isPromo, gender ) VALUES ( '{$name}' , '{$sub}' , '{$status}' , '{$brand}' , '{$price}' , '{$discount}' , NOW() , '{$category}' , '{$status}' , '{$description}' , '{$id}' , '{$weight}' , '{$insurance}' , '{$frame}' , '{$fork}' , '{$transmission}' , '{$brakes}' , '{$is_discount}' , '{$promo}' , '{$gender}' )";

但现在,我又增加了几列,它就停止工作了。。。有人看到我丢失的东西吗?新查询是:
$query = "INSERT INTO products ( name, type, brand, price, discount, dateAdded, categories, status, description, code, weight, insurance, frame, fork, transmission, brakes, isDiscount, isPromo, gender, frontlight, backlight, stem, seatpost, lock, year ) VALUES ( '{$name}' , '{$sub}' , '{$status}' , '{$brand}' , '{$price}' , '{$discount}' , NOW() , '{$category}' , '{$status}' , '{$description}' , '{$id}' , '{$weight}' , '{$insurance}' , '{$frame}' , '{$fork}' , '{$transmission}' , '{$brakes}' , '{$is_discount}' , '{$promo}' , '{$gender}' , '{$front}' , '{$back}' , '{$stem}' , '{$seat}' , '{$lock}' , '{$year}' )";

这真的很奇怪,因为除了插入几个额外的列之外,没有什么区别!提前谢谢!!
编辑:我还不能投票,因为我没有足够的声誉。所以,我想但不行,对不起!从我在stackoverflow上看到的情况来看,你们真的很感激这次投票,我可以理解:)
编辑:非常感谢大家。我对MySQL还不熟悉,不知道lock是个保留字。我更改了那个字段的名称,现在一切正常。对我来说,什么是最好的方式来表扬每一个帮助过我的人??

最佳答案

您必须转义lock,因为它是一个保留字:

INSERT INTO ... `lock`, year) VALUES (...)

正如马可在回答中所说:值括号中的字段比定义要插入的列多。错误就在这里:
INSERT INTO products (name, type, ... )
VALUES ( '{$name}' , '{$sub}' , '{$status}', ...)";

您需要在type之前或之后添加另一个列名,或者从值列表中删除一个列名:substatus

10-06 13:40
查看更多