duanpanzhu2910 2019-05-04 10:21
浏览 47

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

I have created a website with cart for users. when the user clicks the add to cart button, they should be redirected to the cart page with the item displaying. The library.php which process the code for the cart is below:

<?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]);
       }
}

?>

I am passing the value to this page using the button. the button is below

  <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>

when I am passing id like value="12"; then the product is added to cart. but on the index page, I am getting the id like $getRowVenue['id']; I have printed and checked but when I am passing this variable to the button, I am not getting the value, the item is not added to the cart.

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 AT89C51控制8位八段数码管显示时钟。
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题