【发布时间】:2012-03-28 08:36:54
【问题描述】:
有人可以向我解释如何将我输出的数组数据导入到我的数据库中的行中。
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 连接到我的数据库,但我不确定如何将数组中的数据插入到我的数据库中的行中。
谢谢。
【问题讨论】:
-
重组你的数据库,并有一个单独的成分表...不要将它们全部存储在配方表的列中
-
@Mark Baker 感谢您的评论。我目前确实将我的数据库分为成分、食谱标题和食谱项目表。我需要从数组中获取数据并将其插入到配方项目表中,但我不确定如何使用 PDO 做到这一点。任何帮助将不胜感激。