使用SQL中的循环数据保存数据

时间:2017-11-23 10:23:06

标签: php codeigniter

您好我有一个项目并尝试保存正在循环的数据。我在PHP上使用codeigniter作为框架。而且我无法在Loop语句中保存数据。

这是我的模特:

$tdl ='';
    $append = 'insert into collateral3 (clientId, resAreatdl, resNumtdl, resValasstdl, resYearasstdl, comAreatdl, comNumtdl, comAssvaltdl, comYearasstdl, agriAreatdl, agriNumtdl, agriAssvaltdl, agriYearasstdl) values ';
    if (array_key_exists('tdl', $info)) {
        foreach($info['tdl'] as $row) {
            if (!empty($row['resAreatdl'])) {
                $tdl .= $append ."('$clientId', upper('{$row['resAreatdl']}'), upper('{$row['resNumtdl']}'), upper('{$row['resAssvaltdl']}'), upper('{$row['resYearasstdl']}'), upper('{$row['comAreatdl']}'), upper('{$row['comNumtdl']}'), upper('{$row['comAssvaltdl']}'), upper('{$row['comYearasstdl']}'), upper('{$row['agriAreatdl']}'), upper('{$row['agriNumtdl']}'), upper('{$row['agriAssvaltdl']}'), upper('{$row['agriYearasstdl']}'))";
            }
            $append =", ";
        }
    }

1 个答案:

答案 0 :(得分:0)

对于多个记录,请使用批量插入

if (array_key_exists('tdl', $info)) {
    foreach($info['tdl'] as $row) {
     $data[] =  array(
       'clientId' => $clientId ,
       'resAreatdl' => $row['resAreatdl'],
       'resNumtdl' => $row['resNumtdl']
        /* add your all field like this*/
       );
    }
 }
 $this->db->insert_batch('collateral3', $data);