我想用php制作购物车。首先,我从sql DB表中获取数据,然后将它们添加到数组中。现在我想将这些数据添加到html表中。每行包含一个名为'添加到购物车的按钮'。当我点击任何按钮('添加到购物车')时,只会将数组的最后一项添加到购物车数组中。我该怎么做。
$query = "SELECT * FROM `item` WHERE 1";
$result = mysqli_query($con,$query);
$k = mysqli_num_rows($result);
$row_array;
while($test_row = mysqli_fetch_array($result)){
$row_array[]=$test_row;
[enter image description here][1]
}
foreach($row_array as $key=>$value){
echo "<tr>";
echo "<td>";
echo $row_array[$key]['item_id'];
echo '<input type="hidden" name="item_id" value="' . $row_array[$key]['item_id'] . '" />';
echo "</td>";
echo "<td>";
echo $row_array[$key]['item_name'];
echo "</td>";
echo "<td>";
echo $row_array[$key]['item_category'];
echo "</td>";
echo "<td>";
echo $row_array[$key]['item_price'];
echo '     <input type = "submit" name = "add_to_cart" class = "btn btn-success" value = "Add to cart" />';
echo "</td>";
echo "</tr>";
}`