-1

我有以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);
?>            
4

2 回答 2

1

嗨,您似乎没有添加任何内容来通过 post 方法。找到下面的代码,我已经向您展示了如何以正确的方式进行操作。这将每次将该选定产品添加到该特定变量。

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" value='.$row_all['name'].' 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 = $_POST["add_to_cart"];

                echo 'Selected Product  = '.$selectedProduct;

            }

    mysqli_close($conn);

但是,如果您希望它每次都添加或迭代产品数量,以便变量存储许多产品。您将不得不使用会话变量

于 2017-04-08T15:10:35.660 回答
0
 <?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);
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" value='.$row_all['name'].' 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 = $_POST["add_to_cart"];

                echo 'Selected Product  = '.$selectedProduct;

            }

        mysqli_close($conn);
?>
于 2017-04-19T17:15:58.030 回答