有人可以請我解釋一下如何將我輸出的數組數據導入到數據庫的行中。使用PDO將數組數據插入到數據庫中
HTML
<form id="AddRecipeForm" method="post" action="includes/add-recipe.php" class="form-inline">
<input type="text" name="recipe[ingredient][1]" class="input-large" placeholder="Title 1"><input type="text" name="recipe[quantity][1]" class="input-large" placeholder="Quantity 1"><br /><br />
<input type="text" name="recipe[ingredient][2]" class="input-large" placeholder="Title 2"><input type="text" name="recipe[quantity][2]" class="input-large" placeholder="Quantity 2"><br /><br />
<input type="text" name="recipe[ingredient][3]" class="input-large" placeholder="Title 3"><input type="text" name="recipe[quantity][3]" class="input-large" placeholder="Quantity 3"><br /><br />
<button type="submit" class="btn">Add Recipe</button>
</form>
這被傳遞到一個PHP形式:
foreach($_POST['recipe'] as $key=>$value)
{
}
print_r($_POST);
和輸出以下的數組:
Array (
[recipe] => Array (
[ingredient] => Array (
[1] => eggs
[2] => milk
[3] => flour
) [quantity] => Array (
[1] => 12
[2] => 13
[3] => 14
)
)
)
我需要將每個單獨的成分和數量導入數據庫表中的新行。我正在使用PDO連接到我的數據庫,但我不確定如何將數據中的數據插入到數據庫的行中。
謝謝。
重組您的數據庫,並有一個單獨的配料表......不要將它們全部存儲在配方表中的列中 – 2012-03-11 20:50:33
@Mark Baker感謝您的評論。我目前有我的數據庫分成成分,食譜標題和食譜項目表。我需要從數組中獲取數據並將其插入到食譜項目表中,但我不確定如何使用PDO來做到這一點。任何幫助將非常感激。 – 2012-03-11 20:57:32