0

2 つの別々のデータベース テーブル (成分と指示) に挿入するスクリプトがあります。1 つ目は を使用$this->db->query($sql)し、2 つ目$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;
    }

2 つの別個の 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ステートメントに結合されます。

2 つの 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 に答える