BD sales record

Asked

Viewed 52 times

1

Good morning.

I’m making a sales web app, and I want to return the sales ID to insert into the intermediate sales table (multiple products in 1 sale). I tried that way, but obviously I couldn’t:

<?php

$connect = mysqli_connect("localhost", "root", "", "narguile");

$number = count($_POST["input_quantidade_venda"]);

$soma_venda = $_POST['soma_venda'];

$data = $_POST['data'];

$hora = $_POST['hora'];

$sql = "INSERT INTO vendas(preco_venda, data, hora) VALUES";
$sql .= "('$soma_venda', '$data', '$hora')";
mysqli_query($connect, $sql);

$id_venda = "SELECT max(id_venda) FROM vendas";

if($number > 0) {

    for($i=0; $i<$number; $i++) {

        if(trim($_POST["input_quantidade_venda"][$i] != '')) {

            $sql2 = "INSERT INTO venda_produto(quantidade, id_venda) VALUES('".mysqli_real_escape_string($connect, $_POST["input_quantidade_venda"][$i])."', '$id_venda')";
            mysqli_query($connect, $sql2);
        }  
    }

    echo "Venda cadastrada!";  
}  

else {

    echo "Não rolou";  
}
?>

If anyone can help, I’d appreciate it...

2 answers

1


mysqli_insert_id - Returns the automatically generated id in the last query.

In a nutshell

if ($result = $connect->query("INSERT INTO vendas(preco_venda, data, hora) VALUES('$soma_venda', '$data', '$hora');")) {
   $id_venda  = $mysqli->insert_id;
}

The function mysqli_insert_id() returns the ID generated by a query in a table with a column with the attribute AUTO_INCREMENT. If the last query is not an INSERT or UPDATE statement or the modified table does not have a column with the AUTO_INCREMENT attribute, this function returns zero

0

If the ID you use in the table vendas is autoincrementable (property AUTO_INCREMENT active) can do as follows:

// ...
$id_venda = mysqli_insert_id($connect);
// ...
$sql2 = "INSERT INTO venda_produto(quantidade, id_venda) VALUES('".mysqli_real_escape_string($connect, $_POST["input_quantidade_venda"][$i])."', '$id_venda')";
mysqli_query($connect, $sql2);
// ...

If it’s not, then it could be:

// ...
$id_venda = "(SELECT MAX(id_venda) FROM vendas)";
// ...
$sql2 = "INSERT INTO venda_produto(quantidade, id_venda) ";
$sql2 .= "SELECT '".mysqli_real_escape_string($connect, $_POST["input_quantidade_venda"][$i])."', $id_venda";
mysqli_query($connect, $sql2);
// ...

There are more "beautiful" ways to do what you want, but it’s up to you whether you want to improve or not :)

  • obg, João... I am using the Ids with AUTO_INCREMENT, so I tried the first code, and presented the following errors: Notice: Undefined variable: mysqli in xxx on line 17 Notice: Trying to get Property of non-object in xxx on line 17 I tried the second code and it worked, although I’m not sure if it might give some kind of conflict in the future... SELECT MAX returns the last line or the highest value?

  • 1

    The MAX returns the highest value. Edited response, test with new code!

Browser other questions tagged

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