-2
$tSQL = "insert into events(title,start,end,allday,url,customerid) VALUES(\"" . $_POST['title'] . "\", FROM_UNIXTIME($epochstart), FROM_UNIXTIME($epochend), \"$allday\", \"$url\", \"$customerid\")";
$mysqli->multi_query($tSQL);
$lasterror = $mysqli->error;



$tSQL = "update events set url = \"details.php?\"" . mysql_insert_id() . " where idevents = \"$eventid\"";
$row = $mysqli->multi_query($tSQL);
$lasterror = $mysqli->error;

echo print_r($tSQL);

我的插入语句确实插入了记录,但是 mysql_insert_id() 一直返回 0。不应该这样,因为该事件表中有一个自动递增的主键,并且运行良好。有关如何获取最后插入的 ID 的任何建议?

4

4 回答 4

4

您的查询是通过 执行mysqli的,因此该mysql函数不会保存插入的 ID。相反,使用mysqli版本

$id = $mysqli->insert_id;
于 2012-12-14T07:07:22.503 回答
4

因为你正在使用mysqli而不是mysql

如果使用程序样式,只需替换mysql_insert_id()mysqli_insert_id()

$mysqli->insert_id或者如果使用面向对象的样式,则将其替换为

于 2012-12-14T07:08:27.137 回答
3

由于您使用的是 mysqli 扩展,因此请更改

$tSQL = "update events set url = \"details.php?\"" . mysql_insert_id() . " where idevents = \"$eventid\"";

$tSQL = "update events set url = \"details.php?\"" .$mysqli->insert_id. " where idevents = \"$eventid\"";
于 2012-12-14T07:07:02.190 回答
1

因为您使用mysqli的是mysql.

使用mysqli->insert_id代替mysql->insert_id()

$tSQL = "insert into events(title,start,end,allday,url,customerid) VALUES(\"" . $_POST['title'] . "\", FROM_UNIXTIME($epochstart), FROM_UNIXTIME($epochend), \"$allday\", \"$url\", \"$customerid\")";
    $mysqli->multi_query($tSQL);
    $lasterror = $mysqli->error;

    $lastInsId=$mysqli->insert_id();

    $tSQL = "update events set url = \"details.php?\"" . $lastInsId . " where idevents = \"$eventid\"";
    $row = $mysqli->multi_query($tSQL);
    $lasterror = $mysqli->error;

    echo print_r($tSQL);
于 2012-12-14T07:11:30.090 回答