0

我创建了一个注册页面,一切正常。只是想让这个小插入返回 id(设置为自动增量)。我该怎么做呢?

$sql = "INSERT INTO accounts (street1, suburb, city, postcode, username) VALUES(?, ?, ?, ?, ?)";

if (! $accres = $connection->prepare($sql)){
    $userinfo = "<font color = red>Account preparation failed: (" . $connection->errno . ") " . $connection->error . "</font>";
    showRegisterForm ( $userinfo );
} else {
    $accres->bind_param("sssss", $address, $suburb, $city, $postcode, $username);
    $accres->execute();
}
4

1 回答 1

2

您可以从连接变量中读取它:

//execute query then:
$id = $connection->insert_id;

http://www.php.net/manual/en/mysqli.insert-id.php

于 2013-11-11T23:53:06.807 回答