如何将购物车中的多个产品作为单独的行插入数据库中

时间:2018-11-27 16:24:05

标签: php mysql codeigniter mysqli

我是对此的初学者,我事先表示歉意。我整夜都在冲浪,试图找到问题的答案。然后我看到了这个面向程序员的平台。

这是我的问题,我的功能运行正常。我有这个购物车,用户选择了很多产品并提交。但这只会在我的数据库中插入一个值。谁能帮我?

购物车

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Shopping_cart extends CI_Controller {

    function index()
    {

        $this->load->model("shopping_cart_model");
        $data["product"] = $this->shopping_cart_model->fetch_all();
        $this->load->view("shopping_cart", $data);

        if (isset($_POST['submitbtn'])) {

            $data = array(
                    "product_name"  => $_POST["cart_name"],
                    "quantity"  => $_POST["cart_qty"],
                    "product_price"  => $_POST["cart_price"]
                );

            $this->db->insert('occasion', $data);
            $this->session->set_flashdata("success", "Your account has been 
        reserved");
            redirect("shopping_cart","refresh");    

        }
    }

    function add()
    {
        $this->load->library("cart");
        $data = array(
                  "id"  => $_POST["product_id"],
                  "name"  => $_POST["product_name"],
                   "qty"  => $_POST["quantity"],
                   "price"  => $_POST["product_price"]
                  );
        $this->cart->insert($data); //return rowid 
        echo $this->view();
    }

    function load()
    {
        echo $this->view();
    }

    function remove()
    {
        $this->load->library("cart");
        $row_id = $_POST["row_id"];
        $data = array(
                'rowid'  => $row_id,
                'qty'  => 0
            );
        $this->cart->update($data);
        echo $this->view();
    }

    function clear()
    {
        $this->load->library("cart");
        $this->cart->destroy();
        echo $this->view();
    }

    function view()
    {
        $this->load->library("cart");
        $output = '';
        $output .= '
        <h3>Shopping Cart</h3><br />
             <div class="table-responsive">
             <div align="right">
             <button type="button" id="clear_cart" class="btn btn-warning">Clear 
             Cart</button>
             </div>
             <br />
             <table class="table table-bordered">
            <tr>
             <th width="40%">Name</th>
             <th width="15%">Quantity</th>
             <th width="15%">Price</th>
             <th width="15%">Total</th>
             <th width="15%">Action</th>
            </tr>';
        $count = 0;
        foreach($this->cart->contents() as $items)
        {
            $count++;
            $output .= '
                <tr> 
                <td>'.$items["name"].' <input type="hidden" id="cart_name" name="cart_name" value="'.$items["name"].'" /></td>
                 <td>'.$items["qty"].' <input type="hidden" id="cart_qty" name="cart_qty" value="'.$items["qty"].'" /></td>
                  <td>'.$items["price"].' <input type="hidden" id="cart_price" name="cart_price" value="'.$items["price"].'" /></td>
                 <td>'.$items["subtotal"].'</td>
                  <td><button type="button" name="remove" class="btn btn-danger btn-xs remove_inventory" id="'.$items["rowid"].'">Remove</button></td>
                  </tr>
                  ';
        }
        $output .= '
               <tr>
               <td colspan="4" align="right">Total</td>
               <td>'.$this->cart->total().'</td>
                </tr>
               </table>

             </div>
             ';

        if($count == 0)
        {
            $output = '<h3 align="center">Cart is Empty</h3>';
        }
        return $output;
    }
}

1 个答案:

答案 0 :(得分:0)

   $data = array(
            "product_name"   => $_POST["cart_name"],
            "quantity"       => $_POST["cart_qty"],
            "product_price"  => $_POST["cart_price"]
    );

    $this->db->insert('occasion', $data);

1)假设您使用$ _POST ['submitbtn']验证了购物车,看来您只是在插入一种产品。

2)为什么您已经从$ _POST收到产品(如果已经将它们存储在卡库中)?

也许这会有所帮助:

foreach($this->cart->contents() as $items)
 {
   $data = array(

        "product_name"   => $items["name"],
        "quantity"       => $items["qty"],
        "product_price"  => $items["price"]

    );
    $this->db->insert('occasion', $data);
  }