繁体   English   中英

MySQL:LAST_INSERT_ID()出现问题

[英]MySQL: Trouble with LAST_INSERT_ID()

我想在同一个查询中插入多个表,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

我不相信你可以用一个查询调用运行多个语句; 你需要调用mysqli_multi_query

http://php.net/manual/en/mysqli.multi-query.php

$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].'"');
}
    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支持

暂无
暂无

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

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