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 somysql_query("insert ...") or die(mysql_error())
, edit the question and add the errors.– rray
The error can be records with duplicate values...
– rray
Opa friend , I made the issue with the error and tested too , really says that is doubling the values
– Matheus Goes
In your bank there must be a Primary Key that cannot be repeated. .
– Pedro Augusto
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?
– rray
fields are auto-increment
– Matheus Goes
You want to take the last recorded record in sales and record in the other tables?
– rray
That, I would like to take the latest records that I insert in the sales table and Ravas in the other two
– Matheus Goes
It is one record at a time entered in right sales?
– rray
Yes and only one record at a time
– Matheus Goes