PHP - Make 3 INSERT in different tables with relationship between them

Asked

Viewed 1,010 times

7

I’d like your approval. I need to do 3 INSERT all at once. Only I also need to get the last ID of each table, to do the relationship of the tables.

That’s right, do it that way?

if($_GET['operacao'] == 'addPrimeiroCadastro'){
    $mysqli->query("INSERT INTO grupo (id_condominio, nome) VALUES ('".$loginUltimoRegistro."','".$_GET['inputPriGrupo']."')");
    $ultimoGrupo = $mysqli->insert_id;

    $mysqli->query("INSERT INTO unidades (id_condominio, id_grupo, nome) VALUES ('".$loginUltimoRegistro."','".$_GET['inputPriGrupo']."')");
    $ultimoUnidade = $mysqli->insert_id;

    $mysqli->query("INSERT INTO morador (id_condominio, id_unidade, funcao, nome, cpf, senha) VALUES ('".$loginUltimoRegistro."', '".$ultimoUnidade."', ,'sin', '".$_GET['inputPriNome']."', '".$_GET['inputPriCPF']."', '".$_GET['inputPriSenha']."')");   
}
  • 2
  • It works more the fact that you did not test whether entered correctly is a problem. It may be that INSERT does not work and it may not get the correct $mysqli->insert_id. If you do not enter $mysqli->insert_id it may be from a previous data entry because you are using a global connection.

1 answer

2

Use two-step transactions to ensure that all three Inserts (group, unit, and resident) are made or none in the event of failure.

if($_GET['operacao'] == 'addPrimeiroCadastro'){

    mysqli_autocommit($conexao, false);
    mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

    try {
        $mysqli->begin_transaction();

        $sql = "INSERT INTO grupo (id_condominio, nome) VALUES (?,?)";
        $stmt = $mysqli->prepare($sql);
        $stmt->bind_param('is', $loginUltimoRegistro, $_GET['inputPriGrupo']);
        $stmt->execute();
        $ultimoGrupo = $mysqli->insert_id;

        $sql = "INSERT INTO unidades (id_condominio, id_grupo, nome) VALUES (?,?,?)";
        $stmt = $mysqli->prepare($sql);
        $nome = "Doge";
        $stmt->bind_param('iis', $loginUltimoRegistro, $_GET['inputPriGrupo'], $nome);
        $stmt->execute();
        $ultimoUnidade = $mypresqli->insert_id;

        $sql = "INSERT INTO morador (id_condominio, id_unidade, funcao, nome, cpf, senha) VALUES (?,?,?,?,?,?)";
        $stmt = $mysqli->prepare($sql);
        $funcao = "sin";
        $stmt->bind_param('iissss', $loginUltimoRegistro, $ultimoUnidade, $funcao, $_GET['inputPriNome'], $_GET['inputPriCPF'], $_GET['inputPriSenha']);
        $stmt->execute();

        $mysqli->commit();
    }catch (mysqli_sql_exception $e){
        echo 'erro ao inserir, código: '. $e->getCode() .' mensagem: '. $e->getMessage();
        $mysqli->rollback();
    }
}
  • You have to turn off autocommit in php no?

  • @Diegofelipe did not understand the question.

  • mysqli_autocommit($conexao, false); You had already hung up, inattention of me :(

  • @Diegofelipe, no problem :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.