First time using commit () and rollback ()

0

Good evening Personally, I use the code below to do two transactions on the bank:

$sqlinsert = $mysqli->prepare("INSERT INTO 
tbl_pessoafisica(cpf,senha,nome,email,celular) VALUES (?,?,?,?,?)");
$sqlinsert ->bind_param('sssss', $cpf,$senha,$nome,$email,$cel);
$sqlinsert ->execute();

if($sqlinsert->affected_rows==1){ // se gravou 

$codpf = $sqlinsert->insert_id;
$sqlinsert->close(); // fecho conexao 

$sqlinsertacesso = $mysqli->prepare("INSERT INTO tbl_acesso (cod_perfil,cod_pf) VALUES (?,?)");
$sqlinsertacesso->bind_param('ii', $perfil,$codpf);
$sqlinsertacesso->execute();

if($sqlinsertacesso->affected_rows==1){ // se gravou na tabela acesso 

    $sqlinsertacesso->close();

}

else{

    $sqlinsertacesso->close(); 
    $sqldelete = $mysqli->prepare("DELETE FROM pessoafisica where cod_pf = '$codpf'");
    $sqldelete->execute();
    $sqldelete->close();

}


}

else{

$sqlinsert->close(); 

}

That is, I write to the personal table, I get the inserted code, then I try to write to the other table, if I can get OK, DELETE the record inserted in the first table. This is working, but I would like to USE commit and rollback. I would like someone to give me an example of how I would do this:

$sqlinsert = $mysqli->prepare("INSERT INTO 
tbl_pessoafisica(cpf,senha,nome,email,celular) VALUES (?,?,?,?,?)");
$sqlinsert ->bind_param('sssss', $cpf,$senha,$nome,$email,$cel);
$codpf = $sqlinsert->insert_id;

$sqlinsert = $mysqli->prepare("INSERT INTO tbl_acessolofe 
(cod_perfil,cod_pf) VALUES (?,?)");
$sqlinsert->bind_param('is', $perfil,$codpf);

Se as duas querys foram executadas
commit()
SENAO
rollback()

The way I connect to the bank, in a file the include("login/conn.php"); part is like this:

$mysqli = new mysqli($servidor, $user, $senha, $banco);

if (mysqli_connect_errno()) {
die('Não foi possível conectar-se ao banco de dados: ' . 
mysqli_connect_error());
exit();
}

Thanks to everyone who can give me an example, if you notice that I need the ID of the first QUERY to enter in the second. $codpf = $sqlinsert->insert_id;

    
asked by anonymous 01.02.2018 / 02:55

0 answers