Php 将行插入两个单独的表时出现MySQL错误
我试图同时将数据插入两个单独的表中,但出现以下错误:Php 将行插入两个单独的表时出现MySQL错误,php,mysql,insert,Php,Mysql,Insert,我试图同时将数据插入两个单独的表中,但出现以下错误: 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 'INSERT INTO StoreTable (Name, Price, Description, ImageName, Colors, Sizes, SKU,' at lin
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 'INSERT INTO
StoreTable (Name, Price, Description, ImageName, Colors, Sizes, SKU,' at line 1
这是我的密码:
// INSERT the NEW Store Item & its image into the database:
$query = "BEGIN; INSERT INTO StoreTable(Name, Price, Description, ImageName, Colors,
Sizes, SKU, Category, DateAdded) VALUES ('". $newStoreItemName ."', '". $newStoreItemPrice ."',
'". $newStoreItemDescription ."', '". $newStoreItemImageName ."',
'". $newStoreItemColors ."', '". $newStoreItemSizes ."', '".$StoreNewItemSKU."',
'".$newStoreItemCategory."', '".$storeItemDateAdded."');
INSERT INTO StoreCategoriesTable (CategoryName, DateModified) VALUES('".$newStoreItemCategory ."',
'". $storeItemDateAdded."'); COMMIT;";
// Echo the query statement to see what it looks like:
echo "Here's the full query:<br/><br/>" .$query. "<br/><br/>";
$result = $mysqli->query($query);
由于我是一名iOS开发人员,对MySQL的了解很少,因此我非常感谢您在这方面提供的任何帮助。谢谢 作为php的一项安全功能,使用
mysqli::query()
一次只能运行一个查询。我建议您将这些查询分解为单独的查询,确保它们正确保存,然后将它们提交到数据库。我们可以找到一个很好的例子
另外:你可以看看,这可以达到类似的效果
我还有一件事要提请注意安全执行这样的查询会使数据库遭受sql注入攻击。我强烈建议您看看如何将参数绑定到查询。(这将自动避开确保查询运行无问题所需的一切)。作为multi_queryok的替代方案,我正在研究数据库事务。。。。但是请原谅我的无知:我应该在那个例子中使用“过程风格”方法,还是“面向对象风格”?看起来你在用面向对象风格作为例子。我建议你继续坚持下去。
BEGIN; INSERT INTO StoreTable (Name, Price, Description, ImageName, Colors,
Sizes, SKU, Category, DateAdded) VALUES ('Shirt', '12.98', '100% Cotton',
'TShirt.Png', 'Red, Blue, Black', 'S, M, L', '88nnkLb', 'Childrens Apparel',
'2015-03-28 15:51:41'); INSERT INTO StoreCategoriesTable (CategoryName,
DateModified) VALUES('Childrens Apparel', '2015-03-28 15:51:41'); COMMIT;