-2
I’m making a website for a school work where I need to create List of products available in the Database, and have the option to enter in the shopping cart.
Until then it is working correctly, but when I click on "finalize purchase" to save the purchase in the database are occurring 2 mysqli errors and I can’t identify the origin.
Below is the part of the code I created for this:
<!-- Inserindo no carrinho de compra -->
<table class="table table-hover" id="tabcar">
<h1>Carrinho de Compras</h1>
<thead>
<tr bgcolor="#AAAAAA">
<td>Codigo</td>
<td>Descriçao</td>
<td>Valor</td>
<td>Qntd</td>
<td >Total R$</td>
<td>Remover</td>
</tr>
</thead>
<form action="?acao=up" method="post">
<tbody>
<?php
if(count($_SESSION['listaproduto']) == 0 ){
?>
<div align="center">
<?php echo "<h3><b>Não ha produtos no carrinho</b></h3>";?>
</div>
<?php
}else{
require("conexao.php");
foreach($_SESSION['listaproduto'] as $id => $qtd){
$sql = "SELECT * FROM produto WHERE codigo= '$id'";
$qr = mysqli_query($conn, $sql) or die (mysqli_error());
$ln = mysqli_fetch_assoc($qr);
$codigo = $ln['codigo'];
$descricao = $ln['descricao'];
$valor = $ln['valor'];
$total = $ln['valor']*$qtd;
$subTotal += $total;
echo '<tr>
<td>'.$codigo.'</td>
<td>'.$descricao.'</td>
<td>'.$valor.'</td>
<td><input type="text" size="2" name="prod['.$id.']" value="'.$qtd.'" /></td>
<td>'.$total.'</td>
<td><a class="glyphicon glyphicon-minus" href="?acao=del&id='.$id.'"></a></td>
</tr>';
}
}?>
</tbody>
<!--Parte para finalizar compra -->
<?php
if(isset($_POST['enviar']))
{
$SqlInserirVenda = mysqli_query($conn, "insert into venda(valorTotal) Values('$subTotal')");
$IdVenda = mysqli_insert_id();
foreach($_SESSION['listaproduto'] as $ProdInsert => $Qtd):
$SqlInserirItens = mysqli_query("Insert into itensvenda(IdVenda,IdProduto,Qtde) Values('$IdVenda','$ProdInsert','$Qtd')");
endforeach;
echo "<script>alert('Venda Concluída')</script>";
}
?>
</table>
<div align = "right">
<form action="" enctype ="multipart/form-data" method="post">
<?php echo '<h3> Total: R$'.number_format($subTotal,2,",",".").' </h3>' ?>
<input type="submit" name="enviar" value="Finalizar Pedido" />
</form>
And these are the two mistakes:
Warning: mysqli_insert_id() expects Exactly 1 Parameter, 0 Given
Warning: mysqli_query() expects at least 2 Parameters, 1 Given in
If any part of the code was missing to help me, inform here and I will send, thank you very much!
The program is waiting for a value for the id, and the query you created is missing one of the two values to be sent
– riki481
Do you have any idea where I’m going wrong?
– Marcelo Augusto Colombo