Php 使用PDO将阵列数据插入数据库
有人能给我解释一下我如何将输出的数组数据导入数据库的行中吗 HTMLPhp 使用PDO将阵列数据插入数据库,php,html,arrays,forms,pdo,Php,Html,Arrays,Forms,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 ty
<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>
并输出以下数组:
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连接到数据库,但我不确定如何将数组中的数据插入数据库中的行中
谢谢。好吧,我的表单结构会有点不同,这样您就可以将ingerdients作为自己的数组进行评估,但使用您拥有的结构:
$db = new PDO($dsn, $user, $pass);
$stmt = $db->prepare('INSERT INTO ingredient (name, quantity) VALUES (?,?)');
// youll want to verify that both arrays have the same number of elements before doing this
// as part of your validation
$ingredients = array_combine($_POST['recipe']['ingredient'], $_POST['recipe']['quantity']);
$errors = array();
foreach($ingredients as $name => $quantity)
{
try {
$stmt->execute(array($name, $quantity));
} catch (PDOException $e) {
$errors[] = array(
'message' => "Could not insert \"$name\", \"$quantity\".",
'error' => $e
}
}
if(!empty($errors)) {
//do something?
}
我假设您的问题是
$\u POST
数组的格式。你有:
[recipe][ingredient][...] and
[recipe][quantity][...]
但是,这并不是按行和列组织数据库的数据库结构:
[recipe][...][ingredient, quantity]
您可以看到[…]
是如何移动的。您需要将数组格式映射到数据库格式。这最容易通过以下方法完成:
运行此操作后,使用print\r
验证一切正常:
print_r($recipes);
现在,您可以使用
$recipes
数组将每一行的数据插入数据库中(我假设您知道如何执行insert SQL查询,因此我不会将其放入答案中)。简单示例,无需进行错误检查:
<?php
$dbc = new PDO(/* ... */);
$stmt = $dbc->prepare("INSERT INTO tbl(ingredient,quantity) VALUES(:ingredient,:quantity);");
$numIngredients = count($_POST['recipe']['ingredient']);
for ($i=1; $i <= $numIngredients; $i++) {
$stmt->execute(array(
':ingredient' => $_POST['recipe']['ingredient'][$i],
':quantity' => $_POST['recipe']['quantity'][$i]
));
}
?>
请注意,通常您应该从0开始计算索引,如果您只编写
recipe[component][]
PHP将自动创建索引。重新构造数据库,并创建一个单独的成分表。。。不要将它们全部存储在配方的一列中table@Mark贝克:谢谢你的评论。我目前确实将我的数据库分为配料表、配方标题表和配方项目表。我需要从数组中获取数据,并将其插入到recipe items表中,但我不确定如何使用PDO实现这一点。任何帮助都将不胜感激。谢谢大家在@prodigitalson上的帖子,上面的代码成功了。非常感谢!
<?php
$dbc = new PDO(/* ... */);
$stmt = $dbc->prepare("INSERT INTO tbl(ingredient,quantity) VALUES(:ingredient,:quantity);");
$numIngredients = count($_POST['recipe']['ingredient']);
for ($i=1; $i <= $numIngredients; $i++) {
$stmt->execute(array(
':ingredient' => $_POST['recipe']['ingredient'][$i],
':quantity' => $_POST['recipe']['quantity'][$i]
));
}
?>