0
I am creating a simple system to make order reservations, where I bring the products from the database through a simple while, but if I try to put brackets in my Ames, example name=name[]
it brings all products by clicking only on one, and if I take off the brackets it adds only the last product, regardless of which product I click.
Shopping Screen in which the user / customer selects the desired item to book.
<?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>
save file.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 Submit add to the order it adds only the last product regardless of which item I select. Could someone help me ?
How $stmt was generated?
– Sveen
forgot @Sveen, I just added it above my FORM
– Victor
Need an expensive form each set of inputs and submits or place the
names
with brackets to send everything at once as an array.– rray
then the strange thing is that at the time of sending it does correctly but only takes the last product from the product table always, but in logic it should not occur this is not? Since I am informing the id inside the while, I thought I would get the respective id to each product
– Victor
you only have one form, as all fields have the same
name
it sends only the last. To send all at once and with a form leave your fields like this:<input type="hidden" name="id[]"
give aprint_r($_POST);
to see how the information was sent.– rray
thanks, I had not thought of the array for this page, I will take the test and send the result here @rray
– Victor
@rray tried in several ways, but without success, this way with brackets I step to pick up all the records in the while by clicking on only a single product
– Victor
blz.... so when you click on Ubmit you just want to get the product id of that Ubmit that?
– rray
Yes I wanted to accumulate the products in a list referring to that user, in a way that he clicked on the Add to Order Submit and, he took the correct product to add to the order is only then the user would go to another page to view the order with each prpduto he added so he would just inform the quantity.
– Victor