Hello developer friends,
I set up a transaction script to insert values into 2 different tables, I put a or die at the end of each INSERT so that the data insertion does not occur in the second table , if an error occurs when inserting the first one.
What has bothered me is that even if a data insertion error occurs and the process dies (not entering the data in the database), the table ID is reserved , however, that in a next successful INSERT, the ID is not sequential from the previous record. NOTE: The two tables have auto_increment in ID.
Is there a way to resolve this issue?
Here is an example of my code:
$mysqli->query('START TRANSACTION') or die($mysqli->error);
$sql = "INSERT endereco (logradouro, numero, complemento, bairro, cidade, uf) VALUES ('".$logradouro."','".$numero."','".$complemento."','".$bairro."','".$cidade."','".$uf."')";
$query = $mysqli->query($sql) or die($mysqli->error);
$idEndereco = $mysqli->insert_id;
$sql = "INSERT contato (email, telefone, telefone_adicional) VALUES ('".$email."','".$telefone."','".$telefoneAdicional."')";
$query = $mysqli->query($sql) or die($mysqli->error);
$idContato = $mysqli->insert_id;
$mysqli->query('COMMIT') or die($mysqli->error);