我有一个 HTML 表单,其中有 2 个动态字段,它们是:
<input type="text" name="ingredients[]" placeholder="Ingredients"/></div>
<input type="text" name="quantity[]" placeholder="Quantity"/></div>
看看这里,对于表单中的输入,数据库表应该是这样的 英格 1 1001 1KG 英格2 1001 2KG 3 1001 3KG
现在检查图像,您就会看到发生了什么
我的 php 文件中有食谱 ID,我只需要一个 PDO 代码来帮助我将“成分”、“数量”和“$recipe_ID”插入一行。
<?php
header('Content-type: application/json');
require_once 'db/pdoconnect.php';
if($_POST)
{
$sth = $con->prepare("SELECT `recipeid` FROM Recipes ORDER BY `recipeid` DESC");
$sth->execute();
$previousid = $sth->fetchColumn();
$offset=1;
$generatingid=$previousid+$offset;
$newid=$generatingid;
//print("Last Id=$previousid\n");
//print("New Id=$newid\n");
$title = $_POST['title'];
$preptime = $_POST['preptime'];
$cocktime = $_POST['cocktime'];
$level = $_POST['level'];
$serving = $_POST['serving'];
$recipetype = $_POST['recipetype'];
$intro = $_POST['intro'];
$details = $_POST['details'];
$image = $_POST['image'];
try
{
$stmt = $con->prepare("INSERT INTO Recipes (recipeid,title,preptime,cocktime,level,serving,recipetype,intro,details,recipeimg) VALUES( :newid, :title, :preptime, :cocktime, :level, :serving, :recipetype, :intro, :details, :image)");
$stmt->bindParam(":newid",$newid);
$stmt->bindParam(":title",$title);
$stmt->bindParam(":preptime",$preptime);
$stmt->bindParam(":cocktime",$cocktime);
$stmt->bindParam(":level",$level);
$stmt->bindParam(":serving",$serving);
$stmt->bindParam(":recipetype",$recipetype);
$stmt->bindParam(":intro",$intro);
$stmt->bindParam(":details",$details);
$stmt->bindParam(":image",$image);
if($stmt->execute()) { //check if main query has been executed
$sql = "INSERT INTO Ingredients VALUES";
for($i = 1 ; $i <= count($_POST['ingredients']) ; $i++){
$sql .= " (:recipeid" .$i. ", :ingredient" .$i. ", :quantity" .$i. "),";
}
// remove the last (,) from the $sql
$sql = rtrim($sql, ',');
$sth = $con->prepare($sql);
// binding parameters
for($i = 1 ; $i <= count($_POST['ingredients']) ; $i++){
$varIng = $_POST['ingredients'][$i];
$varQnty = $_POST['quantity'][$i];
$sth->bindParam(':recipeid' .$i , $newid , PDO::PARAM_STR);
$sth->bindParam(':ingredient' .$i , $varIng , PDO::PARAM_STR);
$sth->bindParam(':quantity' .$i , $varQnty , PDO::PARAM_STR);
}
if ($sth->execute()) {
$response_array['status'] = 'success';
}
}//END OF FIRST IF STATEMENT
else{
$response_array['status'] = 'error';
}
echo json_encode($response_array); //SEND THE RESPONSE
}//END OF TRY
catch(PDOException $e){
echo $e->getMessage();
}
}
?>
最佳答案
假设您在数据库中的问题 ingredient_quantity
中发布的图片中显示了表格名称,并且您说您已经拥有 recipe-ID
PHP Fiddle
<?php
$newid = 'A10';
//considering this is your table shown in the picture
$sql = "INSERT INTO ingredient_quantity VALUES";
for($i = 1 ; $i <= count($_POST['ingredients']) ; $i++){
$sql .= " (:newid" .$i. ", :ingredient" .$i. ", :quantity" .$i. "),";
}
// remove the last (,) from the $sql
$sql = rtrim($sql, ',');
$sth = $con->prepare($sql);
// binding parameters
for($i = 1 ; $i <= count($_POST['ingredients']) ; $i++){
$varIng = $_POST['ingredients'][$i];
$varQnty = $_POST['quantity'][$i];
$sth->bindParam(':newid' .$i , $newid , PDO::PARAM_STR);
$sth->bindParam(':ingredient' .$i , $varIng , PDO::PARAM_STR);
$sth->bindParam(':quantity' .$i , $varQnty , PDO::PARAM_STR);
}
$sth->execute();
?>
<小时/>
编辑1:
对于上面的代码,我有一个错误,因为ingredients[]
数组从0
开始,而不是1
,最终的
将是for
循环的索引未定义
,因此解决这个问题,使最后一个 for
循环如下所示:
for($i = 0 ; $i < count($_POST['ingredients']) ; $i++){
$varIng = $_POST['ingredients'][$i];
$varQnty = $_POST['quantity'][$i];
$j = $i + 1;
$sth->bindParam(':newid' .$j , $newid , PDO::PARAM_STR);
$sth->bindParam(':ingredient' .$j , $varIng , PDO::PARAM_STR);
$sth->bindParam(':quantity' .$j , $varQnty , PDO::PARAM_STR);
}
<小时/>
编辑2:
您可以尝试使用 ?
占位符而不是像这样的命名占位符 PHP Fiddle 2 :
//considering this is your table shown in the picture
$sql = "INSERT INTO ingredient_quantity VALUES";
for($i = 0 ; $i < count($_POST['ingredients']) ; $i++){
$sql .= " ( ? , ? , ? ) , ";
}
// remove the last (,) from the $sql
$sql = rtrim($sql, ',');
$sth = $con->prepare($sql);
// binding parameters
$j = 1;
for($i = 0 ; $i < count($_POST['ingredients']) ; $i++){
$varIng = $_POST['ingredients'][$i];
$varQnty = $_POST['quantity'][$i];
$sth->bindValue( $j , $varIng);
$sth->bindValue( $j + 1, $newid);
$sth->bindValue( $j + 2, $varQnty);
$j += 3;
}
$sth->execute();
关于php - 如何使用 PDO 将 html 输入数组插入到 mysql 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40885089/