Table multiplying the data

2

Well, I've had this problem for some time and I would like the help of you, I have 3 tables that would be sale, vendaproduto, vendaservico. and I insert the sales table items into the other two tables with the following code:

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

As soon as I enter the first line everything is ok, but when I enter the second line, in the two tables (vendaproduct, saleservico) already doubles the number of lines and with each new record the line number doubles, someone can Help me with that?

code all:

<?php
ini_set('default_charset','UTF-8');

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_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());

}

header("location:lista.php");
?>

Tables:

    
asked by anonymous 15.12.2015 / 13:47

1 answer

2

You are entering all the records in the sales table all the time. You only have to add the last record.

Try this:

INSERT INTO vendaproduto (id_venda, produtos) SELECT venda.id_venda, venda.produtos FROM venda  ORDER BY venda.id_venda DESC LIMIT 1
    
15.12.2015 / 14:28