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") ;
?>
Is with
AUTO_INCREMENT
?– Maniero
Yeah, this is like AUTO_INCREMENT
– Matheus Goes
Show the structure of these tables to see if there is something wrong.
– Maniero
It is the structure of the table that is important. What is the difference between the
id
and theid__venda
?– Maniero
That one
header
is out of order. After he does theINSERT
on the tableVENDA
will redirect to this page without executing theINSERTS
below. Also, in this firstINSERT
is the columnid_venda
. Is aPK
withAUTO_INCREMENT
? If so, you don’t have toINSERT
as I said in my reply.– Diego Souza
obg, I’ll pack the code here and see what goes
– Matheus Goes
This error means that you are trying to insert a new record with the same ID that is saved with
auto_increment
.– Ivan Ferrer
Probably
vendaproduto
andvendaservico
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.– Ivan Ferrer
Give a read here, extremely recommended for you: Why should we not use mysql type functions_*?
– Ivan Ferrer