How to do multiple Inserts at once?

Asked

Viewed 918 times

4

Well I have 3 tables in my database, and I do one INSERT on the table sale, and as soon as I insert the data into it I would automatically have to insert it into the tables vendaservicos and in the table vendaproduto that would be that code :

if(isset($_POST['send'])){
$venda = $_POST['num_venda'];
$data = $_POST['data_venda'];
$placa = $_POST['placa'];
$km = $_POST['km'];
$produtos = $_POST['produtos'];
$servicos = $_POST['servicos'];


include ('banco.php');


mysql_query("INSERT INTO venda(id_venda, num_venda, data_venda, placa, km, produtos, servicos)
        values(
            NULL,
            '{$venda}',
            '{$data}',
            '{$placa}',
            '{$km}',
            '{$produtos}',
            '{$servicos}'

                        )
        ");

mysql_query("INSERT INTO vendaproduto (id, id_venda, produtos)
          SELECT venda.id_venda, venda.id_venda, venda.produtos
          FROM venda");

mysql_query("INSERT INTO vendaservico (id, id_venda, servicos)
          SELECT venda.id_venda, venda.id_venda, venda.servicos
          FROM venda") ;

header("location:lista.php");}

The first time I tested the code worked, but now every time I try not insert in the tables vendaproduto and vendaservico, only enter the data in the sales table.

The error is as follows: Duplicate entry '1' for key 'PRIMARY'

The fact that I still wear the mysql_ can interfere with something in my code?

  • For testing, always review the headers() and make your query so mysql_query("insert ...") or die(mysql_error()), edit the question and add the errors.

  • The error can be records with duplicate values...

  • Opa friend , I made the issue with the error and tested too , really says that is doubling the values

  • In your bank there must be a Primary Key that cannot be repeated. .

  • The problem is that your select is without Where, so it will return all records from that table to insert. In these tables there are auto-increment or Unique key fields?

  • fields are auto-increment

  • You want to take the last recorded record in sales and record in the other tables?

  • That, I would like to take the latest records that I insert in the sales table and Ravas in the other two

  • It is one record at a time entered in right sales?

  • Yes and only one record at a time

Show 5 more comments

3 answers

3


Use the function mysql_insert_id to get the last record inserted in sales. Then you can do the Insert with select but this time add a Where, which compares the id to the last entered record

if(mysql_query("INSERT INTO venda ... ")){
    $id = mysql_last_id();

    mysql_query("INSERT INTO vendaproduto (id, id_venda, produtos)
          SELECT venda.id_venda, venda.id_venda, venda.produtos
          FROM venda WHERE id = $id") or die(mysql_error());

    mysql_query("INSERT INTO vendaservico (id, id_venda, servicos)
          SELECT venda.id_venda, venda.id_venda, venda.servicos
          FROM venda WHERE id = $id") or die(mysql_error());  
}

-1

Apparently the error you’re making is that you’re trying to insert duplicate values into a unique value field.

But to insert multiple values with an Insert just do so:

INSERT INTO `usuarios` (`id`, `nome`, `email`) VALUES (123, 'Fulano da Silva', '[email protected]'), (32, 'Ciclano', '[email protected]')

-2

You are entering duplicate Ids into the table vendaproduto and vendaservico. If the column "id" is auto-incremental you can omit the ID column.

It would look like this:

mysql_query("INSERT INTO vendaproduto (id_venda, produtos)
      SELECT venda.id_venda, venda.produtos
      FROM venda");

mysql_query("INSERT INTO vendaservico (id_venda, servicos)
      SELECT venda.id_venda, venda.servicos
      FROM venda") ;
  • I tried so , now inserted only that inserted even more , was inserted 5 lines more type duplicating

Browser other questions tagged

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