简体   繁体   English

使用PHP Post中的PDO执行多个MySQL查询

[英]Execute Multiple MySQL Queries using PDO from PHP Post

I have a PHP script to post the following data to add-rma-process.php after submission: 我有一个PHP脚本,可在提交后将以下数据发布到add-rma-process.php

$_POST['rmanumber']
$_POST['parent']
$_POST['qty']

However, there are also other fields which are to be posted but will depend on the $_POST['qty'] variable. 但是,还有其他要发布的字段,但将取决于$_POST['qty']变量。 Say, the $_POST['qty'] = 5 then I will have $_POST['pn1'], $_POST['sn1'], $_POST['rm1'] up to $_POST['pn5'], $_POST['sn5'], $_POST['rm5'] . 假设$_POST['qty'] = 5那么我将拥有$_POST['pn1'], $_POST['sn1'], $_POST['rm1']$_POST['pn5'], $_POST['sn5'], $_POST['rm5'] I think you guys get the logic. 我想你们听懂了。

Once add-rma-process.php receives these data, I am doing this: 一旦add-rma-process.php收到这些数据,我就可以这样做:

require("common.php");
for($i=0; $i<$_POST['qty']; $i++) {
    $count = $i+1; // to start with 1 instead of 0
    $query = "INSERT INTO rmadb (rmanumber, parent, childpn, childsn, remarks, user, date) VALUES (:rmanumber, :parent, :childpn, :childsn, :remarks, :user, NOW())";
    $query_params = array(
        ":rmanumber" => $_POST['rmanumber'],
        ":parent" => $_POST['parent'],
        ":childpn" => $_POST['pn$count'],
        ":childsn" => $_POST['sn$count'],
        ":remarks" => $_POST['rm$count'],
        ":user" => $_SESSION['user']['fname']." ".$_SESSION['user']['lname']
    );
        try { 
            $stmt = $db->prepare($query);
            $res = $stmt->execute($query_params);
        } catch(PDOException $ex) {
            die("Failed to run query: " . $ex->getMessage());
        }
}

What I was trying to do is do a for loop to execute the query until the condition is met but it is not working. 我试图做的是做一个for loop来执行查询,直到满足条件但不起作用。 What seems to be wrong? 怎么了?

  1. You should use double quotes here so that key can be evaluated 您应该在此处使用双引号,以便可以评估密钥

     $_POST["pn$count"] ^ ^ 
  2. You don't need to introduce $count variable. 您不需要引入$count变量。 Change condition in for 在变化的条件for
  3. You should prepare your query once and then execute it multiple times with different parameters. 您应该准备一次查询,然后使用不同的参数多次执行它。 That's the whole point behind prepared statements. 这就是准备好的语句的全部要点。 Preventing sql injections is just a beautiful side effect. 防止sql注入只是一个美丽的副作用。

That being said your might look something like this 话虽这么说,你可能看起来像这样

require("common.php");

$query = "INSERT INTO rmadb (rmanumber, parent, childpn, childsn, remarks, user, date) VALUES (:rmanumber, :parent, :childpn, :childsn, :remarks, :user, NOW())";
$stmt = $db->prepare($query);
for ($i = 1; $i <= $_POST['qty']; $i++) {
    $query_params = array(
        ":rmanumber" => $_POST['rmanumber'],
        ":parent"    => $_POST['parent'],
        ":childpn"   => $_POST["pn$i"],
        ":childsn"   => $_POST["sn$i"],
        ":remarks"   => $_POST["rm$i"],
        ":user"      => $_SESSION['user']['fname']." ".$_SESSION['user']['lname']
    );
    $res = $stmt->execute($query_params);
}

Anytime you see yourself naming inputs like rm1, rm2, etc. know that that this is a clear anti-pattern. 每当您看到自己为rm1,rm2等输入命名时,都知道这是一个清晰的反模式。 You should be using array access notation for your input names like: 您应该对输入名称使用数组访问符号,例如:

<input name="rm[]" ... />

PHP will automatically take all inputs with same name and compile into an array that is available in $_POST - so $POST['rm'] and so forth. PHP将自动将所有具有相同名称的输入并编译成$_POST可用的数组-因此$POST['rm']依此类推。

This would simplify you loop to something like 这将简化您循环到类似

$count = $_POST['qty']; // not shown you probably want to validate this value before using it
for ($i = 0; $i < $count; $i++) {
    $query_params = array(
        ":rmanumber" => $_POST['rmanumber'],
        ":parent"    => $_POST['parent'],
        ":childpn"   => $_POST['pn'][$i],
        ":childsn"   => $_POST['sn'][$i],
        ":remarks"   => $_POST['rm'][$i],
        ":user"      => $_SESSION['user']['fname']." ".$_SESSION['user']['lname']
    );
    $res = $stmt->execute($query_params);
}

Note that since I am guessing you are using some kind of javascript in your form to create X number of input fields based on the value in qty, this saves you a lot of headache in javascript in trying to number each input field. 请注意,由于我猜您在表单中使用某种javascript基于qty中的值创建X个输入字段,因此在尝试为每个输入字段编号时,这为您节省了很多麻烦。 You can easily just clone the same input field (or template for the input field) and insert it into the DOM X times without the need to individually change it's one's name property. 您可以轻松地克隆相同的输入字段(或输入字段的模板)并将其插入DOM X次,而无需单独更改其name属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM