如何在PHP中循环$ _POST

时间:2018-08-26 01:59:00

标签: php

我当前正在创建一个简单的预订系统。用户下了一个预购单,他们的订单被插入带有哈希值的pre_orders表中,而cc表被更新为相同的哈希值。

我在管理面板中添加了一个表格,管理员可以在发布当天将所有预购的数字商品发布给所有买家。我希望表单检查pre_orders中的哈希是否与cc中的哈希匹配,如果匹配,则更新该记录,如果没有,则将approved设置为1然后它将跳过并转到下一个进行检查。我看到我需要某种循环,但不确定从哪里开始。

if(isset($_POST['release'])){
        $stmtt = $auth_user->runQuery("SELECT * FROM `pre_orders`");
        $stmtt -> execute(array($_SESSION['user_session']));
        $hashh = $stmtt -> fetch();
        $hash = $hashh['hash'];

        $stmttttt = $auth_user->runQuery("SELECT COUNT(*) FROM `cc` WHERE `hash` = :hash");
        $stmttttt -> execute(array(':hash' => $hash));
        $count = $stmttttt -> fetch();

        $stmttt = $auth_user->runQuery("SELECT * FROM `cc` WHERE `hash` = :hash");
        $stmttt -> execute(array(':hash' => $hash));
        $check = $stmttt -> fetch();
        $idd = $check['id'];

        if($count == true){     

        $stmt = $auth_user->runQuery("UPDATE `cc` SET `approved` = 1 WHERE `id` = :id"); //remove `approved` at end of pre order period
        $stmt -> execute(array(':id' => $idd));
        $success = "Pre-orders released successfully.";

        } else {
            $error = "error";
        }
    }

1 个答案:

答案 0 :(得分:2)

您可以在一个更新语句中完成此操作:

UPDATE cc AS c
INNER JOIN pre_orders AS p ON p.hash = c.hash
SET c.approved = 1
WHERE c.approved <> 1

内部联接将仅查找具有匹配哈希的预购和cc行的行。如果您想查看“受影响的行”以确定更新了多少个新订单,则WHERE子句只是一点点修饰。希望您将cc.approved值默认为0而不是NULL。

如果允许cc.approved为NULL,则需要WHERE子句

WHERE c.approved IS NULL OR c.approved <> 1

您需要的具体技术取决于您在PHP(mysqli或PDO)中使用的客户端库。