2013-03-29 58 views
0

我有一个插入两个独立数据库表的脚本:配料和方向。 对于第一个,我使用$this->db->query($sql),对于第二个:$this->db->query($sql2)(我正在使用CodeIgniter)。 这里是我的代码:数据库插入是否合并?

foreach($_POST as $key => $value) { 
     $value = $this->input->post($key); 
     $directions = $this->input->post('directions'); 
     $ingredientQTY = $this->input->post('ingredientQTY'); 
     $measurements = $this->input->post('measurements'); 
     $ingredientNAME = $this->input->post('ingredientNAME'); 
     $ingredientsROW[] = array($ingredientQTY, $measurements, $ingredientNAME); 

     //For inserting ingredients 
     for ($i = 0, $count = count($ingredientQTY); $i < $count; $i++) { 
      $rows[] = array(
       'ingredientamount'  => $ingredientQTY[$i], 
       'ingredientType'  => $measurements[$i], 
       'ingredientname'  => $ingredientNAME[$i], 
       'recipe_id'    => $recipe_id, 
       'order'     => $i + 1, 
       'user_id'    => $user_id 
      ); 
      $sql = "INSERT `ingredients` (`ingredientamount`,`ingredientType`,`ingredientname`,`recipe_id`, `listOrder`, `user_id`) VALUES "; 
      $coma = ''; 
      foreach ($rows as $oneRow) { 
       $sql .= $coma."('".implode("','",$oneRow)."')"; 
       $coma = ', '; 
      } 

     } 
     $this->db->query($sql);//Insert Query for ingredients 

     //For inserting directions 
     for ($i = 0, $count = count($directions); $i < $count; $i++) { 
      $rows[] = array(
       'direction'  => $directions[$i], 
       'recipe_id'    => $recipe_id, 
       'order'     => $i + 1, 
       'user_id'    => $user_id 
      ); 
      $sql2 = "INSERT `directions` (`direction`,`recipe_id`,`listOrder`,`user_id`) VALUES "; 
      $coma = ''; 
      foreach ($rows as $oneRow) { 
       $sql2 .= $coma."('".implode("','",$oneRow)."')"; 
       $coma = ', '; 
      } 
     } 
     $this->db->query($sql2); //Insert Query for directions 
     break; 
    } 

我应该有两个单独的SQL语句,但出于某种原因,他们组合成,并生成以下错误:

Column count doesn't match value count at row 1 

INSERT `directions` (`direction`,`recipe_id`,`listOrder`,`user_id`) VALUES ('1','Bunch','Cilantro','1','1','1'), ('3','Cup','Sugar','1','2','1'), ('First, combine the cilantro and sugar','1','1','1'), ('then eat. ','1','2','1') 

应该有一个INSERT ingredients以及,但它的值合并到INSERT directions声明中。

为什么要将这两个SQL语句组合起来?

+0

@PreetSangha,有2个($方向) – Muhambi

回答

1

代码上半年建立$rows插入所有成分,那么下半年建立$rows插入四面八方,但它从来没有清除了$rows插图中。当你去做指导时,成分仍然在阵列中。

另外,我认为你经常调用SQL。您的for $i循环和foreach $rows循环不应嵌套;他们应该是一个接着一个。尝试是这样的:

for ($i = 0, $count = count($ingredientQTY); $i < $count; $i++) { 
    $rows[] = array(
    'ingredientamount' => $ingredientQTY[$i], 
    'ingredientType' => $measurements[$i], 
    'ingredientname' => $ingredientNAME[$i], 
    'recipe_id'  => $recipe_id, 
    'order'   => $i + 1, 
    'user_id'   => $user_id 
); 
} // <-- FIRST FOR LOOP SHOULD END HERE 

$sql = "INSERT `ingredients` (`ingredientamount`,`ingredientType`,`ingredientname`,`recipe_id`, `listOrder`, `user_id`) VALUES "; 
$coma = ''; 
foreach ($rows as $oneRow) { 
    $sql .= $coma."('".implode("','",$oneRow)."')"; 
    $coma = ', '; 
} 
// } <-- FIRST FOR LOOP USED TO END HERE; PROBABLY NOT RIGHT 

$this->db->query($sql);//Insert Query for ingredients 

$rows = array(); // <-- Clear out $rows to reuse it for directions 

随着$rows阵列清除出去,那么你可以去到的方向。与成分一样,将循环一个接一个,而不是嵌套。

+0

修复了这个问题,使我的代码更好,谢谢! – Muhambi

-1
$sql2 = "INSERT directions ('direction','recipe_id','listOrder','user_id') VALUES "; 

第一组括号指定4列,但您尝试插入6个值。

固定码:

$sql2 = "INSERT INTO directions VALUES "; 
+0

感谢您的答案!我从行数组中获取值,所以实际上有4列和4个值,所以这是行不通的。 – Muhambi

+0

来自错误的6个值实际上应该插入到成分声明中,但出于某种原因被添加到插入方向而不是 – Muhambi

+0

更改'$ sql2 =“INSERT INTO方向VALUES”中的'directions';到'成分':) :) –