在CodeIgniter中保存多个HTML textareas

时间:2013-05-18 08:52:47

标签: php codeigniter codeigniter-2

我想获取几个textarea字段的值并将它们保存到数据库中。现在我有四个具有不同的值,我想批量保存这些值,textarea字段是:

<textarea  name="compedia[]"></textarea>
<textarea name="specification[]"></textarea>

和保存功能:

function saveCOA(){
    $labref=$this->uri->segment(3);
    $data=  $this->input->post('compedia');
    $data1=  $this->input->post('specification');
    $compedia=array(
        'labref'=>$labref, //NDQA201303001
        'compedia'=>$data,
        'specification'=>$data1
    );
    foreach ($compedia as $value) {
        $this->db->insert('coa_body',$value);  
    }

}

当我print_r($value)返回时:

NDQA201303001
Array ( [0] => Alphy [1] => poxy [2] => alphy [3] => poxy )
Array ( [0] => poxy [1] => alphy [2] => poxy [3] => alphy )

当我尝试保存时,它返回:

A Database Error Occurred

Error Number: 1054

Unknown column 'NDQA201303001' in 'field list'

INSERT INTO `coa_body` (`NDQA201303001`) VALUES ('')

Filename: C:\xampp\htdocs\NQCL\system\database\DB_driver.php

Line Number: 330

语法应该如何循环遍历所有textarea值并立即将它们保存到数据库中?

2 个答案:

答案 0 :(得分:2)

我希望

count($data) == count($data1); //Always True!

如果是这种情况,以下情况将起作用:

for ($i=0;$i<count($data);$i++) {
    $insert_data = array(
        'labref'=>$labref, //NDQA201303001 - Same for all the rows
        'compedia'=>$data[$i],
        'specification'=>$data1[$i]
    );
    $this->db->insert('coa_body',$insert_data);
}

检查此链接:CodePad.org

<小时/> 的更新: 建议Rcpayan

//This will reduce number of context switching,
//even though loping is doubled!
for ($i=0;$i<count($data);$i++) {
    $insert_data[$i] = array(
        'labref'=>$labref, //NDQA201303001
        'compedia'=>$data[$i],
        'specification'=>$data1[$i]
    );
}
$this->db->insert_batch('coa_body',$insert_data);

答案 1 :(得分:2)

你可以做这样的事情,它有点长,但也可以处理 compedia 规范不相等。这个解决方案假设了一些事情:

  • 您希望 labref 的值对于插入的每一行都相同
  • 如果 compedia 规范的值不相等,您仍然希望插入行,但“缺失”值将设置为{ {1}}。

NULL

或者,您可以更改循环以将值分配给数组,然后批量插入这些值。这可能会提供更好的性能。

$labref             = $this->uri->segment(3);
$compedia_data      = $this->input->post('compedia');
$specification_data = $this->input->post('specification');

//Calculate which array is larger, so we can loop through all values
$max_array_size = max(count($compedia_data), count($specification_data));

//Iterate through the arrays
for ($i = 0; $i < $max_array_size; $i++)
{
    $this->db->set('labref', $labref);

    //If we still have a value(s) for compedia, then assign the value, otherwise set to NULL
    if array_key_exists($i, $compedia_data)
    {
        $this->db->set('compedia', $compedia_data[$i]);
    }
    else
    {
        $this->db->set('compedia', NULL);
    }        

    //If we still have a value(s) for specification, then assign the value, otherwise set to NULL
    if array_key_exists($i, $specification_data)
    {
        $this->db->set('specification', $specification_data[$i]);
    }
    else
    {
        $this->db->set('specification', NULL);
    }

    //Insert into table: 'coa_body'
    $this->db->insert('coa_body');
}