MYSQL query:
$sql_product = "SELECT * FROM products WHERE product_id= $product_id";
$sql_result = $dbh->query($sql_product);
PHP:
foreach($sql_result as $row)
{
$product_id=$row['product_id'];
$product_name=$row['product_name'];
$price=$row['price'];
$picture=$row['picture'];
// output
echo "
<div>$product_name</div>
<div>$price</div>
<div>
<form action='cart.php' method='POST' id='buy'>
Amount: <select name='amount'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select>
</form>
</div>
";
if (isset($_POST['amount']))
{
$amount = $_POST['amount'];
echo $amount, '<br /><br />';
}
}
echo "<button type='submit'class='btn btn-primary' form='buy'>Buy</button>";
I have a problem with the option tag in the PHP foreach loop. It's a shopping cart where there is an HTML option tag inside a form. The value from the option tag is then made into a variable called $amount. But whenever I press the button the amount changed for all the products in the shopping cart. So if I have 3 products in the cart and I want to buy 1 of the first and 2 of the second and third, then the variable $amount changes to 1 for all products. Can anyone help me out?