0

我想获取几个 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 值并将它们一次保存到数据库中?

4

2 回答 2

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);
于 2013-05-18T11:00:00.857 回答
2

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

  • 您希望labref的值对于插入的每一行都相同
  • 如果compedia规范的值的数量不相等,您仍想插入行,但“缺失”值将设置为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');
}

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

//Initial other relevant code is included in the example above (excluded here for brevity)
$insert_array = new array();

//Iterate through the arrays
for ($i = 0; $i < $max_array_size; $i++)
{
    $row_array           = new array();
    $row_array['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)
    {
        $row_array['compedia'] = $compedia_data[$i];
    }
    else
    {
        $row_array['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)
    {
        $row_array['specification'] = $specification_data[$i];
    }
    else
    {
        $row_array['specification'] = NULL;
    }

    //Add current row to the insert array, so it can be added to the database
    $insert_array[$i] = $row_array;
}

//Insert into table: 'coa_body'
$this->db->insert_batch('coa_body', $insert_array);
于 2013-05-18T12:06:06.313 回答