我想在同一个查询中插入多个表,table2从table1中检索id
这是我的sql代码
$q = "
INSERT INTO Product (pName, pBrand, pCategory, pSize, pQuantity, pPrice, pDetail)
VALUES('$name', '$brand', '$category', '$size', '$quantity', '$price', '$detail');
INSERT INTO Image (iName, iExt, iSize, pID)
VALUES('$img_name', '$img_ext', '$img_size', LAST_INSERT_ID());";
$mysqli->query($q);
它显示了语法错误。 但是我将$ q的输出复制到在phpMyAdmin的SQL查询中运行它有效。 你有没有人能指出我的错误在哪里?
INSERT INTO Product (pName, pBrand, pCategory, pSize, pQuantity, pPrice, pDetail)
VALUES(....); #1 row affected
INSERT INTO Image (iName, iExt, iSize, pID)
VALUES(....); #1 row affected
答案 0 :(得分:2)
我不相信你可以用一个查询调用运行多个语句;您需要拨打mysqli_multi_query
来代替:
答案 1 :(得分:0)
$q = "
INSERT INTO Product (pName, pBrand, pCategory, pSize, pQuantity, pPrice, pDetail)
VALUES('$name', '$brand', '$category', '$size', '$quantity', '$price', '$detail');
INSERT INTO Image (iName, iExt, iSize, pID)
VALUES('$img_name', '$img_ext', '$img_size', LAST_INSERT_ID());";
$arr=explode(";",$q)
for($i=0;isset($arr[$i]);$i++)//or use limit($arr) in place of isset
{
$mysqli->query('"'.$arr[$i].'"');
}
答案 2 :(得分:0)
By default, mysql_query() and mysql_real_query()
interpret their statement string argument
as a single statement to be executed, and you process
the result according to whether
the statement produces a result set
(a set of rows, as for SELECT)
or an affected-rows count (as for INSERT, UPDATE, and so forth).
阅读mysql的C API支持