【问题标题】:loop through update query pdo where id循环更新查询 pdo where id
【发布时间】:2015-10-30 00:37:53
【问题描述】:

我正在尝试各种方法来循环遍历 MySQL 调用过程查询 (PDO)

在以下示例中,我将更新表/数据库中的 一列。通过表单内的 HTML 表提交的 $_POST 数据。

我计划做一些其他的“表格数据”表格,并循环通过 MySQL 调用过程,其中要更新的列不止一列

所有更新查询都将基于WHERE ID = $id

我在学校使用嵌套的 foreach 循环做过类似的事情,但我在某处读到,在将数据发布到 mysql 时应该避免使用嵌套的 foreach 循环

  • 在这种情况下,有大量表格数据的最佳方法是什么 要通过他们的主键 id 更新?

  • 是否可以对 MySql 查询进行大规模循环,如果可以
    这里最好使用哪些循环? While vs Foreach、For、Do 等?

  • 那么什么是最好的 bindParam 与 bindValue,占位符?对比 姓名等?


/* Call up data based on some criteria while loop fetch it etc */

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) // Works and echoes some tabular data O.K

/* Snippet of the POST data concerned */
<td>
    <input type="text" name="allocate[]">
    <input type="hidden" name="id[]" value="<?php echo $ID; ?>">
</td>


/* Echoes something of the following HTML table inside form O.K */

<tr>
    <td><input type="text" name="allocate[]"><input type="hidden" name="id[]" value="1244"></td>
    <td>echo stuff</td>
    <td>echo unrelated</td>
</tr>
<tr>
    <td><input type="text" name="allocate[]"><input type="hidden" name="id[]" value="1255"></td>
    <td>echo stuff</td>
    <td>echo unrelated</td>
</tr>
<tr>
    <td><input type="text" name="allocate[]"><input type="hidden" name="id[]" value="1399"></td>
    <td>echo stuff</td>
    <td>echo unrelated</td>
</tr>

/* The submitted form part */
<?php

$db = DBConn::getConnection(); // config files etc etc

if ( isset( $_POST['id'] ) && isset( $_POST['allocate'] )) {  // works

    // NOTE: no validation required for trusted "In-house" data entry 

    /* Simple method/varibles OR ?? */
    $id = $_POST['id'];
    $allot = $_POST['allocate'];

    /* OR ?? implode and explode array etc etc version-method (execute(array(.. ?? */

    $id = explode(",",$_POST['id']);
    $allot = explode(",",$_POST['allocate']);

    /* The Update Query */

    //$sql = "UPDATE propertyStats SET allot = :allot WHERE ps_id = :ps_id";
    //$sql = "UPDATE propertyStats SET allot = ? WHERE ps_id = ?";

    $sql = 'CALL updatePropSat(?,?)'; // Call procedure tested & works OK in phpMyAdmin

    $stmt = $db->prepare($sql);


    /* executing in an array rather foreach loop method? */
    $stmt->execute(array($allot, $id));


    /* FOREACH ATTEMPT /* NOTE: Form HTML was different to above HTML example */

    foreach ($_POST['allocate'] as $ids => $value) {

        $sql = 'CALL updatePropSat(?,?)';

        $stmt = $db->prepare($sql);

        $stmt->bindValue(1,  $value, PDO::PARAM_STR);
        $stmt->bindValue(2,  $ids,   PDO::PARAM_INT);


        $stmt->execute();

        /* OR ALTERNATELY ?? */
        $stmt->execute(array($allot, $id));

    }

    /* WHILE LOOP ATTEMPT ( a flop I can few )*/

    $loopit = count($id); // did return correct number rows in the table/form

    $i = 0;
    while($i < $loopit) {

        $stmt->bindParam(':allot', $allot[$i]);
        $stmt->bindParam(':ps_id', $id[$i]);

        $stmt->execute();

        $i++;
    }

【问题讨论】:

    标签: mysql pdo foreach while-loop http-post


    【解决方案1】:

    在这种情况下,通过主键 ID 更新大量表格数据的最佳方法是什么?

    准备好的声明。重要的部分是在循环外准备它们一次,然后在循环内执行它们内部。在您的示例中,您正在循环中准备它,这违背了目的:

    $stmt = $db->prepare('CALL updatePropSat(?,?)');
    foreach ($_POST['allocate'] as $ids => $value) {
        $stmt->execute([
            $value,
            $ids,
        ]);
    }
    

    是否可以对 MySql 查询进行大规模循环,如果可以 这里最好使用哪些循环? While 与 Foreach、For、Do 等?

    这取决于原因数据。没有绝对的答案。如果您将prepare() 保留在循环之外,而将execute() 保留在其中应该没问题。我大部分时间都使用 foreach,因为我很少有足够的数据超过内存限制。

    那么这里最好的 bindParam 与 bindValue, placeholders 是什么? vs名称等?

    这是一个偏好问题。当我在单个查询中要更改许多字段并且将它们按正确的顺序放置很烦人时,我会使用命名占位符。但我更喜欢定位占位符。

    【讨论】:

    • 感谢您的回答。抱歉回复缓慢,不知何故错过了帖子通知并且很忙。现在还是新的,无论如何我一开始确实被卡住了..方括号返回了一个错误。所以在我尝试的foreach里面: $stmt->execute(array($allot, $id));但后来我还不得不更改表单名称 T0:code From: 偏离路线在循环外执行 prepare() 是线索。谢谢!
    • 但是这只是更新日期的一列 - 我想知道有多少列会是什么样子?嵌套的 Foreach 循环??? (我希望不会)例如。 UPDATE tablename SET col1 = col1, col2 = col2, col3 = col3 (etc etc) WHERE id = id
    【解决方案2】:

    我发现你所有的问题都无关紧要。

    我可以从您的问题中提取的唯一问题是嵌套的 foreach 循环。我能想到的唯一这样的循环是一个更新同一行的列,而不是一次更新,而是每个查询一个。这个问题与 PDO 或任何“最佳方法”无关。这是普通的旧 SQL。您必须在一个查询中更新同一行中的所有列。因此,您不需要嵌套的 foreach 来运行您的查询。

    这里最好使用哪些循环? While 与 Foreach、For、Do 等?

    无关紧要。没有“最佳循环”。

    bindParam vs bindValue, placeholders 最好的是什么? vs名称等?

    无关紧要。它们都一样。

    调用 updatePropSat(?,?)

    无关紧要。不需要 SP 来执行简单的更新。只需使用 PDO 运行更新查询


    现在有一些提示。

    为您的单个值填充您的表单,如下所示

    <td>
        <input type="text" name="allocate[<?php=$ID?>]">
    </td>
    

    您将能够使用其他答案中的代码进行循环:

    $stmt = $db->prepare('UPDATE propertyStats SET allot = ? WHERE ps_id = ?');
    foreach ($_POST['allocate'] as $id => $value) {
        $stmt->execute(array($value, $id));
    }
    

    同时对于属于同一行的多个值

    <td>
        <input type="text" name="data[<?php=$ID?>][allocate]">
        <input type="text" name="data[<?php=$ID?>][foo]">
        <input type="text" name="data[<?php=$ID?>][bar]">
        <input type="text" name="data[<?php=$ID?>][id]">
    </td>
    

    通过这种方式,您将能够以最简单的方式遍历数据

    $sql = "UPDATE t SET allocate = :allocate, foo = :foo, bar = :bar WHERE id = :id";
    $stmt=$pdo->prepare($sql);
    foreach ($_POST['data'] as $row)
    {
        $stmt->execute($row);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      相关资源
      最近更新 更多