I'm trying to save the auto incremented ID that'll be created in the table 'accounts' when a new user filled out the registration form in the second table 'userdaten'.
The code saves the data in both tables like it should, but I still need to get the auto incremented 'id' from table 'accounts' into my table 'userdaten'
if ($stmt = $con->prepare('INSERT INTO accounts (username, password, email) VALUES (?, ?, ?)')) {
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$stmt->bind_param('sss', $_POST['username'], $password, $_POST['email']);
$stmt->execute();
echo 'Data saved in table accounts';
$stmt2 = $con->prepare('INSERT INTO userdaten (name, vorname, gebdatum, strasse, hnr, plz, ort) VALUES (?, ?, ?, ?, ?, ?, ?)');
$stmt2->bind_param('sssssss', $_POST['name'], $_POST['vorname'], $_POST['gebdatum'], $_POST['strasse'], $_POST['hausnummer'], $_POST['plz'], $_POST['ort']);
$stmt2->execute();
echo 'Data saved in table userdaten';
I tried to make it work with mysqli_insert_id
for the past hour, but I couldn't make it work.
Any idea what I have to change? the code above gives no error and is functional, with the exception that the auto incremented 'id' is only saved in 'accounts'.