1つのpdo文の出力を別の文の配列データで使用したいと考えています。現時点では、両方のステートメントセットは個別にうまく動作しますが、データベースの1つのテーブルに出力をマージする方法がわかりません。INSERT用の2つのPDO文をデータベースに結合するにはどうすればよいですか?
私が更新しようとしているデータベーステーブルは、recipe_id
,item_number
およびquantity
の3つのカラムを持っています。
をメインrecipe_id
として使用し、私の配列の出力を他の2つの列に使用する必要があります。うまくいけば、私が使用していたコードは、コメントを以下のようになり、私が意味を作っていますし、誰かが助けることができる:
<?php
//MySQL Database Connect
require 'config.php';
//Takes form input for recipe title for insert to the recipe table
$name = $_POST["recipeName"];
//Stored procedure inputs the recipe name to the recipe table and outputs a recipe_id which is to be passed into recipe item table below
$stmt = $dbh->prepare("CALL sp_add_recipe(:name, @output)");
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
//Execute Statment
$stmt->execute();
//$recipeID variable stores recipe_id outputted from the stored procedure above
$recipeID = $dbh->query("SELECT @output")->fetchColumn();
//Insert places the values from $recipeID, item_number & quantity into the recipe_item table
$stmt = $dbh->prepare('INSERT INTO recipe_item (recipe_id, item_number, quantity) VALUES (:recipeID,?,?)');
$stmt ->bindParam(':recipeID',$recipeID, PDO::PARAM_STR);
//Ingredients variable combines array values from HTML form
$ingredients = array_combine($_POST['recipe']['ingredient'], $_POST['recipe']['quantity']);
//Each value from the form is inserted to the recipe_item table as defined above
foreach($ingredients as $name => $quantity)
{
$stmt->execute(); //I would like to insert $recipeID to my database with each line of the array below.
$stmt->execute(array($name, $quantity));
}
?>
このシナリオこれ以上に 'mysql_escape_string'を使用する必要はありません(実際には、それは間違っていると文字列を壊す可能性があります)。 PDOは、準備されたステートメントでエスケープする仕事をしています –
@Pekkaは更新をありがとう、私は自分のコードを更新しますが、PDOを把握しています。どのように私が出力を得ることについて行くかもしれないかについてのあなたの考えがあるなら、それは大いに感謝されるだろう。 –
現在のコードで何が問題になっていますか? – galymzhan