如何在php中使用mysqli获取最后一个插入ID?

时间:2015-09-27 13:05:04

标签: php mysql mysqli

我想将数据插入两个表,第二个表有一个字段table1_id。 我在php中写了一段代码:

if ($insert_client_stmt = $mysqli->prepare("INSERT INTO client (username, email, password) VALUES (?, ?, ?)")) {
    $insert_client_stmt->bind_param('ssss', $username, $email, $passwordt);
    if (! $insert_client_stmt->execute()) {
        header('Location: ../error.php?err=Registration failure: INSERT');
    }
    $client_id = $mysqli->lastInsertId();
}





if ($insert_client_details_stmt = $mysqli->prepare("INSERT INTO client_addr (addr_id, client_id, client_name, contact_name) VALUES (?, ?, ?, ?)")) {
$insert_client_stmt->bind_param('ssss', $client_id, $_POST['name'], $_POST['contact']);
// Execute the prepared query.
if (! $insert_client_stmt->execute()) {
    header('Location: ../error.php?err=Registration failure: INSERT');
}

}

当我运行它时,我收到以下错误:

Fatal error: Call to undefined method mysqli::lastInsertId() in myfile.php on line 91

第91行是这一个:

$client_id = $mysqli->lastInsertId();

如何解决这个问题并获得新创建的client_id?

0 个答案:

没有答案