How to write MYSQL data in two tables at the same time

0

How to write MYSQL data in two tables at the same time, but the second table named "t_cadplots" should insert more than one record according to the number of plots so I implemented the for but I'm having compilation errors, which is the best method to record these records?

It is also necessary to capture the last record from the inserted table a moment before "t_cadcontracts" so that I can insert in the second table "t_cadplots".

Connection to database:

    <?php include"../Connections/config.php";
    $conexao = mysqli_connect("$hostname_config","$username_config","$password_config", "$database_config");
    if (mysqli_connect_errno())
      {
      echo "Erro ao selecionar o banco de dados, por favor informe o administrador do sistema ([email protected]) ou envie um email para [email protected] !" . mysqli_connect_error();
      }

// Set autocommit to off
mysqli_autocommit($conexao,FALSE);
?>

Insert's:

<div id="painelcadastro2" align="center">
<?php   if (isset($_GET['cadastra']) && $_GET['cadastra'] == 'add') {
  $datacompra = implode("-", array_reverse(explode("/",$_GET['datacompra'])));
  $codigoProduto = filter_input(INPUT_GET, 'identProduto1');
  $nomeProduto = filter_input(INPUT_GET, 'nomeProduto1');
  $qtProduto = filter_input(INPUT_GET, 'qtProduto1');
  $valorProduto = filter_input(INPUT_GET, 'valorProduto1');
  $valorparc = filter_input(INPUT_GET, 'valorparcela');
  $parcelas = filter_input(INPUT_GET, 'select_parcelas');
  $entrada = filter_input(INPUT_GET, 'entrada');
  $total = filter_input(INPUT_GET, 'total');
  $nrFicha = filter_input(INPUT_GET,'cadastro');
  $status = "ativo";
  $dataVencimento = filter_input(INPUT_GET, 'datasvenc');
  $dataVencimento = unserialize(base64_decode($dataVencimento));//Decode para array
  $pagamento = "CREDIARIO";
  $cadastracontratos = mysqli_query($conexao, "INSERT INTO t_cadcontratos (Ficha, DataContrato, QuantParcelas, ValorContrato, Entrada, Saldo, DescricaoProduto, QuantProdutos, Vendedores, FormaPagamento) 
                          VALUES ('$nrFicha', '$datacompra', '$parcelas', '$valorProduto', '$entrada', '$total', '$nomeProduto', '$qtProduto', UPPER('$_SESSION[MM_Username]'), '$pagamento')");
  for($numparcelas=1; $numparcelas>$parcelas; $numparcelas++ ){
  $cadastraparcelas = mysqli_query($conexao, "INSERT INTO t_cadparcelas (NumContrato, NumParcela, ValorParcela, DataVencimento, Status) 
                          VALUES ('$NumContrato', '$numparcelas', '$valorparc', '$dataVencimento[numparcelas]', '$status')");
  }
  if($cadastracontratos == '1' && $cadastraparcelas == '1') {
    // Commit transaction
mysqli_commit($conexao);
        echo "Venda Crediário realizada com sucesso !";
  }else{
        echo "Erro ao realizar a venda Crediário, tente novamente !";
  }
// Close connection
mysqli_close($conexao);
}
?>
    
asked by anonymous 06.11.2014 / 17:31

1 answer

1

Yes, you need to use% ID of loop , use the ID generated by the last query.

$ultimoID = mysql_insert_id();

I suggest for testing that you check for each insertion in the base if it was successful, adding up the number of successful plots and testing in the end.

Now you have some important remarks:

  • MySQL functions in PHP will be discontinued, you should use the PDO or < MySQLi instead of MySQL.

  • MySQLi does not change much of anything you use in MySQL. It changes very little, take a look.

Another issue is:

  • You can not trust that all queries executed by the insert will be executed. In this case, you must do a transaction ( transaction ), that is, start the transaction, execute the queries, if any fails, you give a rollback at all. So you will not have garbage at the base and you will be sure that everything is ok. Please read about transactions .
07.11.2014 / 01:13