Error " Duplicate entry '1' for key 'PRIMARY' "

Asked

Viewed 1,016 times

3

Good guys to with the following problem , when entering the data in a table I automatically insert the same data in the other two tables , but just right in the first row that I insert , when I insert another row appears the following error :

Duplicate entry '1' for key 'PRIMARY'

Using the following code :

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

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

So I tried like this :

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

mysql_query("INSERT INTO vendaservico (id_venda, servicos)
      SELECT venda.id_venda, venda.servicos
      FROM venda")or die(mysql_error());

only that every time I insert the data in the table it duplicates the data , someone knows where I am missing or can give me a help , obg.

Codigo Inteiro :

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}'

                            )
            ");


    header("location:lista.php");

}


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") ;

?>

  • 1

    Is with AUTO_INCREMENT?

  • 1

    Yeah, this is like AUTO_INCREMENT

  • 1

    Show the structure of these tables to see if there is something wrong.

  • 1

    It is the structure of the table that is important. What is the difference between the id and the id__venda?

  • That one header is out of order. After he does the INSERT on the table VENDA will redirect to this page without executing the INSERTS below. Also, in this first INSERT is the column id_venda. Is a PK with AUTO_INCREMENT ? If so, you don’t have to INSERT as I said in my reply.

  • obg, I’ll pack the code here and see what goes

  • This error means that you are trying to insert a new record with the same ID that is saved with auto_increment.

  • Probably vendaproduto and vendaservico already have the record recorded and so will give error... you are not checking if it has already been recorded... besides, you must be with rules in this bank, so it does not accept more than one foreign key.

  • 4

    Give a read here, extremely recommended for you: Why should we not use mysql type functions_*?

Show 4 more comments

1 answer

3


If the primary key column is auto incremented, you do not need to put it in the INSERT. Reset your table and run the tests again.

Comments on a INSERT and rotate the page. Then comment on the other INSERT and rotate the page. See if it duplicates.

Ah, and post the PHP of your code as well. Sometimes there may be something before it is causing this, like a while or for.

  • 1

    So Zoom did it, oh problem is this is duplicating the data.

  • 1

    I posted all the Code , and now I’ll try to change my table to see if it’s right

Browser other questions tagged

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