0

I want to insert data to two tables and the second table has a field table1_id. I wrote a piece of code in 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');
}

}

and when I run it, I get the following error:

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

line 91 is this one:

$client_id = $mysqli->lastInsertId();

How can I fix that and get the freshly created client_id?

randomuser1
  • 2,733
  • 6
  • 32
  • 68

0 Answers0