Request only the last product registered

0

I am creating a simple system to place order reservations, where I bring the products of the database through a simple while, but if I try to put brackets in my names, example name=name[] it brings all products when clicking on only one , and if I shot the brackets it just adds the last product, regardless of which product I click.

Purchasing screen in which the user / client selects the desired item to reserve.

<?php
    $stmt = $pdo->query('SELECT id, codigo, descricao, preco FROM produtos'); 
?>

<form method="POST" action="salvar_pedido.php" class="">  

        <div class="row">

            <?php while ($produtos = $stmt->fetchObject()){ ?>

                <div class="col-sm-6 col-md-4">

                    <div class="container">

                        <img style="width:75%;" src="imagem.php?id=<?php echo $produtos->id ?>" />

                        <div class="caption">
                                <input type="hidden" name="id" value="<?php echo $produtos->id ?>"> <br>
                                <input type="hidden" name="codigo" value="<?php echo $produtos->codigo ?>"> <br>
                                <strong>Descrição:</strong> <?php echo $produtos->descricao ?> <input type="hidden" name="descricao" value="<?php echo $produtos->descricao ?>">  <br>
                                <strong>Preço: </strong>  R$: <?php echo number_format($produtos->preco,2) ?> <input type="hidden" name="preco" value="<?php echo $produtos->preco ?>"> <br><br>
                                <input type="submit" class="btn btn-primary" value="Adicionar ao Pedido" id="comprar" name="comprar">
                        </div>
                        <br><br>
                    </div>

                </div>

            <?php } ?>

        </div>

</form>

file save_order.php

    <?php

        if (isset($_POST['comprar'])){

                $id = $_POST['id'];
                $pedido = $_POST['pedido'];
                $codigo = $_POST['codigo'];
                $descricao = $_POST['descricao'];
                $preco = $_POST['preco'];
                $status = $_POST['status'];

                $sql = "SELECT * FROM pedido  WHERE pedido = '$pedido'"; 
                $resulta = $conn->query($sql);
                $row = $resulta->fetch_assoc();

                $result = "INSERT INTO pedido (pedido, data_venda, codigo, descricao, preco_venda, status) VALUES ('$pedido', NOW(), '$codigo', '$descricao', '$preco', '$status')";
                $resultado = mysqli_query($conn, $result);   

                if($resultado){
                    echo "<script>redirectPedido()</script>";
                    echo "<script>alert('Pedido Realizado com Sucesso');</script>";
                }  else  {
                    echo "<script>redirectPedido()</script>";
                    echo "<script>alert('Erro ao Realizar Pedido');</script>";
                }

        }

    ?>

Whenever I click on Submit add to the order it adds only the last product regardless of which item I select. Could someone help me?

    
asked by anonymous 23.04.2018 / 16:25

1 answer

1

To solve this problem you should review the logic of creating your form.

In this code it does a while printing several times with same ID fields, which causes the field to be duplicated and in the submit it always assumes the value of the last item.

UPDATED Solution with the most recent information included in the question:

What has been indicated will be a SUBMIT button for each item to be added to the cart, so there should be a form for each item, as follows:

<?php
    $stmt = $pdo->query('SELECT id, codigo, descricao, preco FROM produtos'); 
?>
        <div class="row">
            <?php while ($produtos = $stmt->fetchObject()){ ?>
              <form method="POST" action="salvar_pedido.php" class="">  
                <div class="col-sm-6 col-md-4">
                    <div class="container">
                        <img style="width:75%;" src="imagem.php?id=<?php echo $produtos->id ?>" />
                        <div class="caption">
                                <input type="hidden" name="id" value="<?php echo $produtos->id ?>"> <br>
                                <input type="hidden" name="codigo" value="<?php echo $produtos->codigo ?>"> <br>
                                <strong>Descrição:</strong> <?php echo $produtos->descricao ?> <input type="hidden" name="descricao" value="<?php echo $produtos->descricao ?>">  <br>
                                <strong>Preço: </strong>  R$: <?php echo number_format($produtos->preco,2) ?> <input type="hidden" name="preco" value="<?php echo $produtos->preco ?>"> <br><br>
                                <input type="submit" class="btn btn-primary" value="Adicionar ao Pedido" id="comprar" name="comprar">
                        </div>
                        <br><br>
                    </div>
                </div>
              </form>
            <?php } ?>
        </div>

Previous answer considering that it would be a SUBMIT that would send all records:

You have two solutions: Create an array of "similar" fields as @rray said or an ID for each form field.

To do this, just stop and test the logic. Think of the inputs as variables ... if you put $ code = 987, then put $ code = 99999, at the end the variable will only have value 99999 and not both.

    
24.04.2018 / 13:41