插入行数和输入post用法


Inserting number of rows and input post usage

我正在尝试将数据插入for循环中的表:

public function insertprolist()
{     
    for ($i = 1; $i < $this->input->post('numrows'); $i = $i + 1)
    {
        $data = array(
            'productname' => $this->input->post('proname'+$i),
            'quantity' => $this->input->post('quantity'+$i),
            'price' => $this->input->post('price'+$i),
            'amount' => $this->input->post('amount'+$i)
        );
        return $this->db->insert('purchaseprolist', $data);   
    }
}

我得到一个错误。

没有插入到数据库表中

我已经添加了带有名称如proname1, quantity1的输入字段的新行,并且对于第二行,输入名称属性将生成为proname2, quantity2,并以此类推。

我正在检索另一个名称为numrows的输入中的行数。

现在我想在模态文件中访问这些输入

for ($i = 1; $i < $this->input->post( 'numrows' ); $i++)
{
  $data = array(
              'productname' => $this->input->post( 'proname' . $i ),
              'quantity'    => $this->input->post( 'quantity' . $i ),
              'price'       => $this->input->post( 'price' . $i ),
              'amount'      => $this->input->post( 'amount' . $i )
               );
   $this->db->insert('purchaseprolist', $data);   
 }

这很可能会工作,但是它正在为每一行做插入,你最好看看建立一个值数组,并使用insert_batch()一次完成整个事情。


更好的选择:

for ($i = 1; $i < $this->input->post( 'numrows' ); $i++)
{
  $data[] = array(
              'productname' => $this->input->post( 'proname' . $i ),
              'quantity'    => $this->input->post( 'quantity' . $i ),
              'price'       => $this->input->post( 'price' . $i ),
              'amount'      => $this->input->post( 'amount' . $i )
               );
 }
 $this->db->insert_batch( 'purchaseprolist', $data );

这将执行单个数据库插入查询,而不是循环行数并执行每行插入。将相同的数据提交到数据库只会更有效。

也许你应该使用设置你的"+" = "."PHP会理解这是一个匹配。

for ($i = 1; $i < $this->input->post('numrows'); $i = $i + 1)
{
  $data = array(
    'productname' => $this->input->post('proname'.$i),
    'quantity' => $this->input->post('quantity'.$i),
    'price' => $this->input->post('price'.$i),
    'amount' => $this->input->post('amount'.$i)
);
   return $this->db->insert('purchaseprolist', $data);
}

因为你的问题不清楚,所以我只能这样向你解释

也许问题是您正在尝试在forloop内return。所以试着把它移到

下面
public function insertprolist()
{     
    for ($i = 1; $i < $this->input->post('numrows'); $i = $i + 1)
    {
        $data = array(
            'productname' => $this->input->post('proname'+$i),
            'quantity' => $this->input->post('quantity'+$i),
            'price' => $this->input->post('price'+$i),
            'amount' => $this->input->post('amount'+$i)
        );
        $this->db->insert('purchaseprolist', $data);
    }
}

在PHP中,您不能使用+号进行连接,您需要在这里使用(.)点:

$data = array(
        'productname' => $this->input->post('proname'.$i),
        'quantity' => $this->input->post('quantity'.$i),
        'price' => $this->input->post('price'.$i),
        'amount' => $this->input->post('amount'.$i)
        );

第二个,你正在使用return,这将在第一次迭代后停止你的函数,你不能在for循环中使用它。

第三,在插入之前,试着检查你的post值你得到什么,这将帮助你理解你在尝试什么

print_r($_POST); // before for loop start.

第四,而不是多个查询执行,我建议你使用batch_insert(),这将帮助你:http://www.codeigniter.com/userguide3/database/query_builder.html