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 语句?

4

2 回答 2

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阵列后,您可以继续前进。与成分一样,将循环一个接一个地放置,而不是嵌套。

于 2013-03-29T01:58:20.430 回答
-1
$sql2 = "INSERT directions ('direction','recipe_id','listOrder','user_id') VALUES ";

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

固定代码:

$sql2 = "INSERT INTO directions VALUES ";
于 2013-03-29T01:43:43.827 回答