php/sql:如果表a和表b更新后满足条件,则更新表c

ve7v8dk2  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(329)

我现在的代码有些问题。例如,在一个php页面上,有一个表显示所有借用用户的工具。在该表中,每个数据行都包含一个复选框。用户可以通过勾选复选框并按下“返回”按钮来选择他们想首先返回的任何工具。
在服务器端,单击“return”按钮后,它将进入名为return\u selected.php的页面。在这个页面上,它将更新表a和表b。这次成功了。
现在,我想更新表c,如果有一个条件,例如,所有工具都返回了。这一次我仍然做,但失败了。下面是代码
返回\u selected.php

<?php

    include("../../../../config/configPDO.php");
    include("../../../../config/check.php");

    $tools_id = $_POST['tools_id'];
    $borrow_id = $_POST['borrow_id'];

    $checkbox=explode( ',', $_POST['ids'][0] );

    for($i=0;$i < count($checkbox); $i++){
        $tools_id=$checkbox[$i];

        $sql="UPDATE ets_tools SET borrow_id = NULL WHERE tools_id=:tools_id";
        $query=$conn->prepare($sql);
        $query->execute(array(':tools_id' => $tools_id));

        $sql2="UPDATE ets_tools_borrow SET time_to = GETDATE() WHERE tools_id=:tools_id";
        $query3=$conn->prepare($sql2);
        $query3->execute(array(':tools_id' => $tools_id));

        // want to update table if all tools returned.

        $query2 = "
           SELECT 
              ets_tools.tools_id, ets_tools.tools_name, ets_tools.borrow_id,
              ets_borrow.time_from, ets_borrow.time_to, ets_borrow.status_id
           FROM ets_tools 
           INNER JOIN ets_borrow ON ets_tools.borrow_id = ets_borrow.borrow_id
           WHERE ets_tools.borrow_id IS NOT NULL AND ets_borrow.borrow_id = :borrow_id
        ";

        $sql2=$conn->prepare($query2);
        $sql2->execute(array(':borrow_id' => $borrow_id));

        if($sql2->rowCount() > 0)
        {

            header("Location: return.php");

        }else{

            $sql3="UPDATE ets_borrow SET time_to  = GETDATE(), status_id = 2 WHERE borrow_id=:borrow_id";
            $query3=$conn->prepare($sql3);
            $query3->execute(array(':borrow_id' => $borrow_id));

            header("Location: return.php");

        }

    }

?>

有人知道怎么解决这个问题吗?谢谢您。

hmmo2u0o

hmmo2u0o1#

您需要考虑以下几点:
如文件所述, PDOStatement::rowCount() 返回受上一个事件影响的行数 DELETE , INSERT ,或 UPDATE 由相应的pdostatement对象执行的语句,对于大多数数据库,从 SELECT 语句未正确返回。所以,你可以用 SELECT COUNT(*) ... 接近这个计数。
对于您的具体情况,您可以尝试使用 UPDATE ... WHERE NOT EXISTS ... 语句来更新 ets_borrow table和跳过这个 SELECT ... FROM ... INNER JOIN ... 声明。
我不确定我是否正确理解 header("Location: return.php"); 在一个 for 循环,可能需要在循环之后重定向。
例如,基于您的代码:

<?php

    include("../../../../config/configPDO.php");
    include("../../../../config/check.php");

    $tools_id = $_POST['tools_id'];
    $borrow_id = $_POST['borrow_id'];

    $checkbox=explode( ',', $_POST['ids'][0] );

    for($i=0; $i < count($checkbox); $i++) {
        $tools_id=$checkbox[$i];

        $sql = "UPDATE ets_tools SET borrow_id = NULL WHERE tools_id = :tools_id";
        $query = $conn->prepare($sql);
        $query->execute(array(':tools_id' => $tools_id));

        $sql2 = "UPDATE ets_tools_borrow SET time_to = GETDATE() WHERE tools_id = :tools_id";
        $query2 = $conn->prepare($sql2);
        $query2->execute(array(':tools_id' => $tools_id));

        $sql3 = "
            UPDATE ets_borrow 
            SET time_to = GETDATE(), status_id = 2 
            WHERE 
                (borrow_id = :borrow_id1) AND
                NOT EXISTS (
                    SELECT 1
                    FROM ets_tools 
                    INNER JOIN ets_borrow ON ets_tools.borrow_id = ets_borrow.borrow_id
                    WHERE ets_tools.borrow_id IS NOT NULL AND ets_borrow.borrow_id = :borrow_id2
                )
            ";
        $borrow_id1 = $borrow_id;
        $borrow_id2 = $borrow_id;
        $query3 = $conn->prepare($sql3);
        $query3->execute(array(':borrow_id1' => $borrow_id1, ':borrow_id2' => $borrow_id2));
    }

    header("Location: return.php");

?>

相关问题