dpr26232 2018-11-27 16:24
浏览 58

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

I am a beginner on this and i apologize in advance. I've been surfing all night long trying to find the answer for my question. Then i saw this platform for programmers.

Here's my problem, My Function are working really fine. I have this shopping cart and user choose many products and submit it. But it only inserting one value to my database. Can anyone help me?

shopping_cart

<?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条回答 默认 最新

  • dongpa2000 2018-11-27 16:44
    关注
       $data = array(
                "product_name"   => $_POST["cart_name"],
                "quantity"       => $_POST["cart_qty"],
                "product_price"  => $_POST["cart_price"]
        );
    
        $this->db->insert('occasion', $data);
    

    1) assuming you validate the shopping cart with $_POST['submitbtn'] It looks like you're just inserting one product.

    2) Why are you receiving product from $_POST if you already store them in card library ?

    Maybe this could help :

    foreach($this->cart->contents() as $items)
     {
       $data = array(
    
            "product_name"   => $items["name"],
            "quantity"       => $items["qty"],
            "product_price"  => $items["price"]
    
        );
        $this->db->insert('occasion', $data);
      }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改