繁体   English   中英

如何重用SQL查询,并根据单击的按钮影响其他行

[英]How to reuse SQL query with a different row affected depending on button clicked

因此,下面有一段代码,我试图将代码的顶部缩减为只有一个SQL查询,该SQL查询每次都以不同的数据库记录运行(每次都需要更改ID) 。

感谢您提供任何帮助,或链接到可以自行学习如何执行此操作的资源的链接,因为我似乎无法用谷歌搜索正确的方法:(

<!DOCTYPE html>
<html>
    <head>
    <?php 
        require_once 'db.php';

        if(isset($_POST['cyanxerox'])){
            $sth = $conn->prepare("UPDATE supplies SET quantity = quantity + 1 WHERE Id=1");
            $sth->execute();
            header('Location: index.php');
            die("Posted, now redirecting");

        }
        if(isset($_POST['magentaxerox'])){
            $sth = $conn->prepare("UPDATE supplies SET quantity = quantity + 1 WHERE Id=2");
            $sth->execute();
            header('Location: index.php');
            die("Posted, now redirecting");

        }
        if(isset($_POST['blackxerox'])){
            $sth = $conn->prepare("UPDATE supplies SET quantity = quantity + 1 WHERE Id=3");
            $sth->execute();
            header('Location: index.php');
            die("Posted, now redirecting");

        }
        if(isset($_POST['yellowxerox'])){
            $sth = $conn->prepare("UPDATE supplies SET quantity = quantity + 1 WHERE Id=4");
            $sth->execute();
            header('Location: index.php');
            die("Posted, now redirecting");

        }

    ?>
    <title>Homepage</title>ss" href="style/
    <link rel="stylesheet" type="text/cmain.css">
</head>
<body>
    <h1>ICT Support Printer Supplies Inventory</h1>
    <form method="POST" action="index.php">
        <input type="submit" name="cyanxerox" value="Cyan Xerox"/>
    </form>
    <form method="POST" action="index.php">
        <input type="submit" name="magentaxerox" value="Magenta Xerox"/>
    </form>
    <form method="POST" action="index.php">
        <input type="submit" name="blackxerox" value="Black Xerox"/>
    </form>
    <form method="POST" action="index.php">
        <input type="submit" name="yellowxerox" value="Yellow Xerox"/>
    </form>

尝试以完整的方式处理准备好的语句,例如,使用适当的验证和异常处理 仅在阅读正在使用的每个PHP函数的文档时 ,您才能实现此目的。 特别是有关数据库访问操作的文档,尤其是文档的“返回值”部分。

您只需要一个带有四个提交按钮的表单。 每个按钮都包含相应的Id值。 所有按钮都具有相同的名称(我选择了“ xerox”)。

我还添加了三个<meta>标记,它们应该出现在所有网页的<head>中。

请注意,您在<title>标签附近有一个错误放置的字符串。

祝好运!

<?php
require_once 'db.php';

if (isset($_POST['xerox'])) {
    $xeroxId = $_POST['xerox'];

    try {
        // The sql statement - it will be prepared.
        $sql = 'UPDATE supplies 
                SET quantity = quantity + 1 
                WHERE Id = :Id';

        /*
         * Prepare and validate the sql statement.
         * If the database server cannot successfully prepare the statement, PDO::prepare() 
         * returns FALSE or emits PDOException (depending on error handling settings).
         */
        $statement = $conn->prepare($sql);
        if (!$statement) {
            throw new UnexpectedValueException('The sql statement could not be prepared!');
        }

        // Bind and validate the binding of the input parameter.
        $bound = $statement->bindValue(':Id', $xeroxId, PDO::PARAM_INT);
        if (!$bound) {
            throw new UnexpectedValueException('An input parameter could not be bound!');
        }

        /*
         * Execute the prepared statement.
         * PDOStatement::execute returns TRUE on success or FALSE on failure.
         */
        $executed = $statement->execute();
        if (!$executed) {
            throw new UnexpectedValueException('The prepared statement could not be executed!');
        }

        /*
         * If the form resides in index.php, then you don't need to do redirect,
         * but just to print a success message.
         */
        // header('Location: index.php');
        // exit();

        $message = 'Data successfully updated';
    } catch (PDOException $exc) {
        echo $exc->getMessage();
        // Only in development phase !!!
        // echo '<pre>' . print_r($exc, TRUE) . '</pre>';
        exit();
    } catch (Exception $exc) {
        echo $exc->getMessage();
        // Only in development phase !!!
        // echo '<pre>' . print_r($exc, TRUE) . '</pre>';
        exit();
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
        <meta charset="UTF-8" />
        <!-- The above 3 meta tags *must* come first in the head -->

        <title>Homepage</title>

        <link rel="stylesheet" type="text/cmain.css">
    </head>
    <body>

        <?php
        if (isset($message)) {
            ?>
            <div class="post-message">
                <?php echo $message; ?>
            </div>
            <?php
        }
        ?>

        <h1>ICT Support Printer Supplies Inventory</h1>
        <form action="index.php" method="POST">
            <button type="submit" name="xerox" value="1">
                Cyan Xerox
            </button>
            <button type="submit" name="xerox" value="2">
                Magenta Xerox
            </button>
            <button type="submit" name="xerox" value="3">
                Black Xerox
            </button>
            <button type="submit" name="xerox" value="4">
                Yellow Xerox
            </button>
        </form>

    </body>
</html>

暂无
暂无

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

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