如何使用PDO将html输入数组插入mysql

时间:2015-11-15 19:52:51

标签: php mysql pdo

我有一个HTML表单,其中有两个动态字段:

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

在这里看一下db表应该如何处理表单中的那些输入 Ing 1 1001 1KG Ing 2 1001 2KG 3 1001 3KG

现在查看图片,您就会看到发生了什么

DB Ingredients TABLE

我的php文件中有食谱ID我只想要一个PDO代码,帮助我插入&#34;成分&#34;,&#34;数量&#34;和&#34; $ recipe_ID&#34;每一个都在一排。

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

?>

1 个答案:

答案 0 :(得分: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开始,index循环的最终for将会出错是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();