【问题标题】:How to insert an html input array into mysql using PDO如何使用 PDO 将 html 输入数组插入 mysql
【发布时间】:2015-11-15 19:52:51
【问题描述】:

我有一个包含 2 个动态字段的 HTML 表单:

<input type="text"  name="ingredients[]" placeholder="Ingredients"/></div>
<input type="text"  name="quantity[]" placeholder="Quantity"/></div>

看这里,对于表单中的那些输入,db 表应该是这样的 英格 1 1001 1KG 英格 2 1001 2KG 3 1001 3KG

现在检查图像,你会看到发生了什么

DB Ingredients TABLE

我的 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();
        }
    }

?>

【问题讨论】:

  • 很好,你有问题吗?
  • 我需要知道怎么做?
  • 您正在使用或尝试插入的查询。

标签: php mysql pdo


【解决方案1】:

假设您在问题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 循环的最终 index 将是 undefined,所以要解决它最后一个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();

【讨论】:

  • 非常感谢,但我需要在这个查询中插入 recipe_id 也请你告诉我把它放在哪里???谢谢
  • 上面的代码 sn-p 用于将值插入图像中的表中,根据您在问题中的代码,我认为您需要将其放在 if($stmt-&gt;execute()) { // returns true on success 之后
  • 是的 recipe_Id 不在数据库中
  • 对于recipe_id,我不确定你从哪里得到它,它和$newid 一样吗?
  • Hello Mi-Creativity 我在第一行添加了以下内容:$sql = "INSERT INTO Ingredients VALUES (recipe_id, ingredients, quantity)";它保存了 $newid 或配方 ID,但对于成分和数量,它一直保存 NULL !你能帮帮我吗
猜你喜欢
  • 2012-04-09
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 2022-08-22
  • 2013-07-27
  • 1970-01-01
  • 2017-07-03
  • 2015-05-21
相关资源
最近更新 更多