Reputation: 19
So I'm trying to set up a simple attendance web app for my company. The check-in/out is done with one button and in order to do that I'm doing an INSERT and an UPDATE almost at the same time. But to do a proper UPDATE I need an 'id' for the 'bind_param', and I'm kinda stuck there. I bet that it's a simple problem, but I can't find my way around it.
if ($pre == 0) {
$sql = $mysqli->prepare("INSERT INTO presence(code_pr,heure_debut,user_id)
VALUES( ? , ? , ? )
") or die(mysqli_error($mysqli));
$sql->bind_param("sss", $code_pr, $heure_debut, $idcompte);
if (!$sql->execute()) {
die('<h3 style="color:red;" align="center">ERREUR</h3>' . mysqli_error($mysqli));
}
} else {
if ($pre == 1 && $precheck == 1) {
$sqlsrt = $mysqli->prepare("UPDATE presence SET heure_fin=? WHERE user_id=? AND id=?") or die(mysqli_error($mysqli));
$sqlsrt->bind_param('ss', $heure_fin, $idcompte);
if (!$sqlsrt->execute()) {
die('<h3 style="color:red;" align="center">ERREUR</h3>' . mysqli_error($mysqli));
}
}
}
I expect to be able to have an id to put in my 'bind_param' .
Upvotes: 1
Views: 81
Reputation: 36
I think that the '$conn->lastInsertId()' will help.
if (!$sql->execute()) {
die('<h3 style="color:red;" align="center">ERREUR</h3>' .
mysqli_error($mysqli));
}else{
//Here you can take the ID of the last Insert
$id = $conn->lastInsertId()
}
Upvotes: 2