我有以下代码。应该返回表的最后一行的mysqli_insert_id()(在本例中为“$ last_row”)总是返回0.为什么这样呢?为什么mysqli_insert_id()总是返回0?
<?php
include('connect-db.php');
$submit_date = date("Ymd");
$content = mysqli_real_escape_string($connection, htmlspecialchars($_POST['editor']));
$ip_address = $_SERVER['REMOTE_ADDR'];
$last_row = mysqli_insert_id($connection);
if ($content != '') {
$sql = "INSERT INTO source (track_id, submit_date, ip, content) VALUES ('$last_row', '$submit_date','$ip_address','$content')";
if (!mysqli_query($connection,$sql))
{
die('Error: ' . mysqli_error($connection));
}
echo $last_row;
mysqli_close($connection);
}
?>
“*返回值:AUTO_INCREMENT字段的值这由前面的查询更新**如果在连接上没有以前的查询,或者查询没有更新AUTO_INCREMENT值,则返回零**。*“ – DCoder 2013-03-11 06:14:13
Look:http://php.net/manual/en /mysqli.insert-id.php – 2015-02-12 01:08:49