Table multiplying the data

Asked

Viewed 140 times

2

Well I’ve had this problem for some time and I’d like your help , have 3 tables that would be sale , vendaproduto , vendaservico . and I insert the items of the sale table in 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 insert the first line everything is ok , more when I insert the second line , in the two tables (vendaproduto, vendaservico) already increases double the number of lines and to each new record the number of line fold , someone can give me a little help in this ?

whole code :

<?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 :

Tabela venda

Tabela vendaproduto

Tabela vendaservico

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

  • vllw by force , I tried in your way and gave the following error "Invalid use of group Function"

  • Try as follows: "INSERT INTO vendaproduto (id_venda, produtos) SELECT venda.id_venda, venda.produtos FROM venda LIMIT 1 ORDER BY venda.id_venda DESC "

  • I return the following error "You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'ORDER BY venda.id_venda DESC' at line 1"

  • POE The "LIMIT 1" after the "DESC", I don’t remember very well the Mysql syntax

  • Thank you so much dude that’s exactly what I needed ;)

  • Good! I put as an answer for you to mark as solved!

Show 2 more comments

1 answer

2


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

Try it this way:

INSERT INTO vendaproduto (id_venda, produtos) SELECT venda.id_venda, venda.produtos FROM venda  ORDER BY venda.id_venda DESC LIMIT 1

Browser other questions tagged

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