I don’t think I am doing this correctly and problem is that because I am using ajax to perform this code in the php script in the background, I can’t check for any errors I may have.
Does the variable $lastID actually retrieve the id from the “ImageId” field which was last inserted or is it not doing this at all? If it is not retrieving the last ID then what do I actually need to do in order to be able to retrieve the last ImageId id?
ImageId is an auto increment field:
Below is code:
$imagesql = "INSERT INTO Image (ImageFile) VALUES (?)";
if (!$insert = $mysqli->prepare($imagesql)) {
// Handle errors with prepare operation here
}
//Dont pass data directly to bind_param store it in a variable
$insert->bind_param("s", $img);
//Assign the variable
$img = 'ImageFiles/' . $_FILES['fileImage']['name'];
$insert->execute();
//RETRIEVE IMAGEID FROM IMAGE TABLE
$lastID = $mysqli->insert_id;
You should use
to get the last inserted id.
Official PHP doc page is here.