在使用 PHP 代码点火器将数据插入数据库时获取空值


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');