我有以while循环形式打印到屏幕上的产品以及与之配套的“add_to_cart”按钮。
我希望能够按下“添加到购物车”按钮并将其上方的选定产品添加到“$selectedProduct”变量中。但是当我按下它时,它什么也没有发生,变量保持空白。
<?php
include('connect.php');
require("session.php");
//create the sql statement
$sql_all=
"SELECT * FROM product
ORDER BY product_ID DESC";
$result_all = mysqli_query($con,$sql_all);
//PRINT OUT ALL THE PRODUCT
while ($row_all = mysqli_fetch_assoc($result_all))
{
echo '<form method="post">';
echo '<u>'.$row_all["name"].'</u>';
echo '<br>';
echo '<button name="add_to_cart" type="submit">Add to Cart</button>';
echo '<hr>';
echo '</form>';
}
if(isset($_POST["add_to_cart"]))
{
//CREATE A VARIABLE THAT HOLDS THE SELECTED PRODUCTED TO BE ADDED TO CART
$selectedProduct = $row_all["name"];
echo 'Selected Product = '.$selectedProduct;
}
mysqli_close($con);
?>