0

My code is working fine but i have a general question regarding the consistency of Last_insert_ID(). My db is stored on online server and many users will use it so i am bit confused about Last_insert_id that can it insert wrong insert id into the linked table. Code in which i have used is below. I am using Transaction. Please clarify.

Note: I have not used AutoCommit=0;

$conn->beginTransaction();

$users_stmt=$conn->prepare("INSERT INTO users (cust_name, u_name, cnic, address, password, email) VALUES (:cust_name, :u_name, :cnic, :address, :password, :email)");
$users_stmt->execute(["cust_name"=>$cust_name, "u_name"=>$u_name, "cnic"=>$cnic_num, "address"=>$address, "password"=>$password, "email"=>$email]);

$var1 = $conn->lastInsertId();
$cell_stmt = $conn->prepare("INSERT INTO cell_num (cell_num,u_id_fk) VALUES (:cell_num, :u_id_fk)");
                                $cell_stmt->execute(["cell_num"=>$cellnum, "u_id_fk"=>$var1]);

$connections_stmt=$conn->prepare("INSERT INTO connections(u_id_fk,pkg_id_fk) VALUES(:u_id_fk,:pkg_id_fk)");
$connections_stmt->execute(["u_id_fk"=>$var1,"pkg_id_fk"=>$pkg_id]);


$conn->commit();
  • The function only returns the most recent autoincremented id value for the most recent INSERT operation, to any table, *on your MySQL connection*, so as long as multiple users aren't using the exact same connection (which is doubtful), it should be fine. – GrumpyCrouton May 14 '20 at 16:33
  • oh thanks so i think i may not need to be worry – Jahanzaib Niazi May 14 '20 at 16:45

0 Answers0