在按钮中作为变量的值未传递到页面

时间:2019-05-04 10:21:03

标签: php html sql

我已经为用户创建了一个带有购物车的网站。当用户单击添加到购物车按钮时,应将他们重定向到显示商品的购物车页面。处理购物车代码的library.php如下:

<?php

// load database connection script
include("database_connection.php");

/*
 * Tutorial: PHP MySQL Shopping cart
 *
 * Page: Application library
 * */

class ShopingCart
{
    protected $db;

    function __construct()
    {
        $this->db = DB();
    }

    /**
     * get products list
     *
     * @return array
     */
    public function getProducts()
    {
        $query = "SELECT *  FROM `entertainment`";
        if (!$result = mysqli_query($this->db, $query)) {
            exit(mysqli_error($this->db));
        }
        $data = [];
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_assoc($result)) {
                $data[] = $row;
            }
        }

        return $data;
    }

    /**
        * get given product details
        *
        * @param [integer] $id
        * @return array
        */
       public function getProductDetails($id)
       {
           $id = mysqli_real_escape_string($this->db, $id);
           $query = "SELECT *  FROM `entertainment` WHERE `id` = '$id'";
           if (!$result = mysqli_query($this->db, $query)) {
               exit(mysqli_error($this->db));
           }
           $data = [];
           if (mysqli_num_rows($result) > 0) {
               while ($row = mysqli_fetch_assoc($result)) {
                   $data['id'] = $row['id'];
                   $data['title'] = $row['title'];
                   $data['price'] = $row['vendor_price'];
                   $data['quantity'] = 1;
               }
           }

           return $data;
       }

       /**
        * Add new product into the cart
        *
        * @param [integer] $id
        * @return void
        */
       public function addToCart($id)
       {

         $product = $this->getProductDetails($id);

         $isFound = false;
         $i = 0;

         if (!isset($_SESSION['shopping_cart']) || count($_SESSION['shopping_cart']) < 1)
         {
             $_SESSION['shopping_cart'] = array(0 => $product);
         } else {

             foreach ($_SESSION['shopping_cart'] as $item) {
                 $i++;
                 foreach ($item as $key => $value) {
                     if ($key == "id" && $value == $id) {
                         array_splice($_SESSION['shopping_cart'], $i - 1, 1, array([
                             'id' => $item['id'],
                             'title' => $item['title'],
                             'price' => $item['vendor_price'],
                             'quantity' => $item['quantity'] + 1,
                         ]));
                         $isFound = true;
                     }
                 }
             }
             if ($isFound == false) {
                 array_push($_SESSION['shopping_cart'], $product);
             }
         }
       }

       /**
        * remove existing product from the cart
        *
        * @param [integer] $id
        * @return void
        */
       public function removeProductFromCart($id)
       {
           unset($_SESSION['shopping_cart'][$id - 1]);
       }
}

?>

我正在使用按钮将值传递给此页面。按钮在下面

  <form method="post" action="cart.php" class="form-inline">
  <input type="hidden" value="' . $product['id'] . '" name="product_id">
  <input type="hidden" value="add_to_cart" name="add_to_cart">
  <button type="submit" name="id"  value="<?echo $getRowVenue['id'];?>" class="btn btn-primary">Add to Cart</button>
  </form>

当我传递像value =“ 12”这样的ID时;然后将产品添加到购物车。 但是在索引页面上,我得到的ID就像我打印并检查过的$getRowVenue['id'];一样,但是当我将此变量传递给按钮时,我没有得到值,该商品未添加到购物车。 / p>

0 个答案:

没有答案