How can LAST_INSERT_ID() be used to retrieve the auto-increment value for insertion into another table in PHP?
To retrieve the auto-increment value generated by a previous INSERT statement in MySQL and use it to insert into another table, you can use the LAST_INSERT_ID() function in PHP. This function returns the last auto-increment value generated for the current session. You can then store this value in a variable and use it in another INSERT statement for another table.
// Perform the first INSERT statement
mysqli_query($conn, "INSERT INTO table1 (column1) VALUES ('value1')");
// Get the last auto-increment value generated
$last_id = mysqli_insert_id($conn);
// Use the last auto-increment value in another INSERT statement
mysqli_query($conn, "INSERT INTO table2 (column1, column2) VALUES ('$last_id', 'value2')");