使用 PHP Codeigniter 将数据插入数据库时​​获取空值

Getting a Null Value when inserting data to database using PHP Codeigniter

我在将我的值插入数据库时​​遇到问题我尝试在我的控制器中使用 echo 但它没有显示任何值这里是我的代码来自我的表单

<form action="<?php echo base_url().'water/save_items' ?>" method="post">
        <table class="purchase">
            <input name="uid" value="5" type="hidden">
            <tbody>
            <tr>
                <th>Qty</th>
                <th>Brand</th>
                <th>Price</th>
            </tr>
            <tr id="row-1">
                <td>4</td><td>Perrier</td><td>216</td>
            </tr>
            <tr id="row-2">
                <td>7</td><td>Volvic</td><td>315</td>
            </tr>
            <tr id="row-3">
                <td>8</td><td>Borjomi</td><td>544</td>
            </tr>
            </tbody>
       <input name="pid[]" value="1" type="hidden"><input name="item[]" value="4" type="hidden">
       <input name="pid[]" value="2" type="hidden"><input name="item[]" value="7" type="hidden">
       <input name="pid[]" value="3" type="hidden"><input name="item[]" value="8" type="hidden">
       </table><br>
    <button type="submit" class="btn btn-success product-button">Checkout</button>
    </form>

控制器

public function save_items()
{
    $uid = $this->input->post('uid');
    $pid = $this->input->post('pid[]');
    $item = $this->input->post('item[]');

    for($x = 0; $x < count($pid); $x++) 
    {
        $data = array("uid"=>$uid, "pid"=>$pid[$x], "item"=>$item[$x]);   
        $this->db->insert('cart', $data);
    }

    $this->load->helper('url');
    $this->load->view('headerlogin');
    $this->load->view('products');
    $this->load->view('footer');
}

"uid" 有一个值,但是 "pid" 和 "item" 在我尝试将它插入我的数据库时有一个空值

您必须将代码更改为:

  $pid = $this->input->post('pid');
  $item = $this->input->post('item');