[wd_asp elements=’search’ ratio=’100%’ id=1]

Get the ID of the last inserted record

11th February 2021

MySql

mysql codehaven category

When you insert a record, you may need to track the id of that inserted record and use it in another table straight after.


$sql = "INSERT INTO phonecalls (clientid,name) VALUES ('$clientid','$name')";

if ($mysqli->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "
" . $sql->error;
}

//if your connection code is this ($mysqli)
//$mysqli = new mysqli('localhost', 'root', 'password', 'dbname');

// then use that in the variable below
echo "ID of last inserted record is: " . mysqli_insert_id($mysqli);