I have a cart and I am working on the button to confirm the purchase. The products arrive to the cart by means of a while, and I keep the data I need from each article in variables. The problem is that when doing the submit only print the values of my last article (I guess that is because the variable is overwritten) I need that if my cart has 2 products at the time of submit, I insert 2 rows in database . This is the while with which I show the selected products in cart:
$direct = "img/libros/";
$sql2 = "SELECT I.ISBN_id,I.titulo,I.imagen,I.precio,U.id,it.idOrdenes,it.libro,it.cantidad,it.usuario FROM lecturas_db.inventario I,lecturas_db.usuarios U,lecturas_db.items it WHERE U.id = it.usuario AND I.ISBN_id = it.libro AND it.usuario = $id";
$res = mysqli_query($conexion,$sql2);
while($row = mysqli_fetch_array($res)) { ?>
<form method="POST" class="Prod">
<div>
<div class="imagenProd">
<img src="<?php echo $direct.$row['imagen'] ?>">
</div>
<div class="tituloProd">
<center><b><?php echo $row['titulo'] ?></b></center>
</div><br><br>
<div class="cantidadProd">
<center><b><?php echo $row['cantidad'] ?></b> unidad/es</center>
</div><br><br>
<div class="subtotalProd">
<center>Subtotal: <b>$<?php $subtotal = $row['precio']*$row['cantidad'];
echo $subtotal;
$total += $subtotal;
?></b></center>
</div><br><br>
<div class="eliminarProd">
<input type="hidden" name="isbn" value="<?php echo $row['libro'] ?>">
<input type="hidden" name="cantidadOrdenes" value="<?php echo $row['cantidad'] ?>">
<input type="hidden" name="usuario" value="<?php echo $row['usuario'] ?>">
<input type="hidden" name="precio" value="<?php echo $row['precio'] ?>">
<input type="hidden" name="id" value="<?php echo $row['idOrdenes']?>">
<input type="submit" value="ELIMINAR" name="eliminar">
</div>
</div><br><br>
<?php if($_POST['eliminar']) {
$id_eliminar = intval($_POST['id']);
$sql3 = "DELETE FROM lecturas_db.items WHERE idOrdenes = $id_eliminar LIMIT 1";
$res1 = mysqli_query($conexion,$sql3);
if($res1 > 0) {
echo "<script>
location.href='index.php';
</script>";
} else {
echo "nada";
}
}
$isbn = $row['libro'];
$cantidadOrdenes = $row['cantidad'];
$usuario = $row['usuario'];
$precio = $row['precio']*$row['cantidad'];
echo $isbn;
echo $cantidadOrdenes;
echo $usuario;
echo $precio;
?>
</form>
<?php } ?>
<form method="POST" class="Prod">
<div>
<div class="imagenProd">
<img src="<?php echo $direct.$row['imagen'] ?>">
</div>
<div class="tituloProd">
<center><b><?php echo $row['titulo'] ?></b></center>
</div><br><br>
<div class="cantidadProd">
<center><b><?php echo $row['cantidad'] ?></b> unidad/es</center>
</div><br><br>
<div class="subtotalProd">
<center>Subtotal: <b>$<?php $subtotal = $row['precio']*$row['cantidad'];
echo $subtotal;
$total += $subtotal;
?></b></center>
</div><br><br>
<div class="eliminarProd">
<input type="hidden" name="isbn" value="<?php echo $row['libro'] ?>">
<input type="hidden" name="cantidadOrdenes" value="<?php echo $row['cantidad'] ?>">
<input type="hidden" name="usuario" value="<?php echo $row['usuario'] ?>">
<input type="hidden" name="precio" value="<?php echo $row['precio'] ?>">
<input type="hidden" name="id" value="<?php echo $row['idOrdenes']?>">
<input type="submit" value="ELIMINAR" name="eliminar">
</div>
</div><br><br>
<?php if($_POST['eliminar']) {
$id_eliminar = intval($_POST['id']);
$sql3 = "DELETE FROM lecturas_db.items WHERE idOrdenes = $id_eliminar LIMIT 1";
$res1 = mysqli_query($conexion,$sql3);
if($res1 > 0) {
echo "<script>
location.href='index.php';
</script>";
} else {
echo "nada";
}
}
$isbn = $row['libro'];
$cantidadOrdenes = $row['cantidad'];
$usuario = $row['usuario'];
$precio = $row['precio']*$row['cantidad'];
echo $isbn;
echo $cantidadOrdenes;
echo $usuario;
echo $precio;
?>
</form>
<?php } ?>