0

同じ名前の複数の入力を持つフォームがあり、結果として配列になります。$_POSTこの配列を処理してデータベースに保存する関数にこの配列を渡したいと思います。

関数を使用せずに問題なくこれを行ったことがありますが、今ではすべてをきちんとした関数呼び出しに含めたいので、それを実行できず、理由がわかりませんか?

問題の input/post 変数には名前が付けられoption[]、 として関数に渡され$_POST['option']ます。関数は次のとおりです。

function newVariant($name, $option) {
global $db;
global $table_prefix;
$table = $table_prefix . "variants";

$query = $db->prepare("INSERT INTO $table(name) VALUES(:name)");
$query->bindParam(":name", $name);

if (!$query->execute()) {
    die(showMessage("Error!","There has been a problem saving your variant. Please try again or contact technical support if the problem persists.",""));
    }

$variant_id = $db->lastInsertId('id');

for($i = 0; $i < count($option); $i++) {
   if($option[$i] != "") {

      $table2 = $table_prefix . "variant_items";
      $query2 = $db->prepare("INSERT INTO $table2(variant, option) VALUES(:variant, :option)");
      $query2->bindParam(":variant", $variant_id);
      $query2->bindParam(":option", $option[$i]);

      if (!$query2->execute()) {
         die(showMessage("Error!","There has been a problem saving your variant. Please try again or contact technical support if the problem persists.",""));
      }
   }
}

$redirect = renderLink("/beyond/?act=admin&sub=variants", "true");
showMessage("Saving variant...<META HTTP-EQUIV=\"Refresh\" Content=\"1; URL=$redirect\">","","");
}

これは、ログに表示されるエラーです。

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'option) VALUES(?, ?)' at line 1' in /Users/adampcollings/Sites/Hot Mint Development/beyond/php/functions/product.php:131
Stack trace:
#0 /Users/adampcollings/Sites/Hot Mint Development/beyond/php/functions/product.php(131): PDO->prepare('INSERT INTO bb_...')
#1 /Users/adampcollings/Sites/Hot Mint Development/beyond/html/admin/new-variant.php(5): newVariant('Test', Array)
#2 /Users/adampcollings/Sites/Hot Mint Development/beyond/php/html.php(19): include('/Users/adampcol...')
#3 /Users/adampcollings/Sites/Hot Mint Development/beyond/html/admin.php(10): subElement('admin', 'new-variant')
#4 /Users/adampcollings/Sites/Hot Mint Development/beyond/php/html.php(8): include('/Users/adampcol...')
#5 /Users/adampcollings/Sites/Hot Mint Development/beyond/index.php(14): siteElement('a in /Users/adampcollings/Sites/Hot Mint Development/beyond/php/functions/product.php on line 131
4

1 に答える 1

2

上記のコメント スレッドに基づいて、試してみるべきことがいくつかあります。

まず、アプリケーションをデバッグするときは常にエラー報告を有効にします。スクリプトでこれを行うには、次を追加します。

error_reporting(E_ALL);

スクリプトの先頭に。

次に、$options期待するデータが含まれていることを確認します。コードのどこかに、次の行を追加します。

var_dump($options);

の内容が表示されます$options。期待する値が含まれていない場合は、送信プロセスを確認してください。

最後に$options、期待されるデータが含まれている場合は、テーブル構造をチェックして、クエリが一致し、正しい値が挿入されていることを確認します。

編集: MySQL エラーを投稿した後、 MySQL 予約語リストをクロスチェックしました。「オプション」という言葉がリストにあります。そのため、単語が列名として認識されないため、クエリは失敗します。列名をバッククォートで囲んでみてください:

$query2 = $db->prepare("INSERT INTO $table2(`variant`, `option`)... 
于 2013-04-22T15:59:48.127 に答える