Finish buying Html + PHP

Asked

Viewed 458 times

-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!

  • 1

    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

  • Do you have any idea where I’m going wrong?

2 answers

0


The function mysqli_insert_id() expects a connector as parameter, i.e.:

$IdVenda = mysqli_insert_id($conn);

and change this line

$SqlInserirItens = mysqli_query("Insert into itensvenda(IdVenda,IdProduto,Qtde) Values('$IdVenda','$ProdInsert','$Qtd')");

for

$SqlInserirItens = mysqli_query($conn, "Insert into itensvenda(IdVenda,IdProduto,Qtde) Values('$IdVenda','$ProdInsert','$Qtd')");
  • 1

    Thanks for the help, fixed this and an error in the comic and it worked. Thanks!

-1

Marcelo, try to make the following change:

$IdVenda = mysqli_insert_id($conn);

  • Mike, I made the change but still errors Warning: mysqli_query() expects at least 2 Parameters, 1 Given in Warning: mysqli_query() expects at least 2 Parameters, 1 Given in

  • Then Marcelo, as riki481 said "The program is waiting for a value, and the query you created is missing one of the two values to be sent", you need to check the names of the variables and see if they are correct.

  • I put the connection parameter in the other msqli_query now it shows the completed purchase message and no error appears, but not saved in the database, any idea? The names of the tables are straight

  • Ah, truth, missed the '$Conn' here '$Sqlinseriritens = mysqli_query($CONN, "Insert into itensvenda(Idvenda,Idproduto,Qtde) Values('$Idvenda','$Prodinsert','$Qtd')";', that you have corrected, right?

  • I fixed yes thanks, but the biggest mistake was that the ID in the database was not auto increment, I changed this and what you said and solved, Thanks for your attention.

  • Ah, I’m glad I helped solve your problem :D

  • 1

    Thank you for

Show 2 more comments

Browser other questions tagged

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