php - 如何根据单击的按钮重用受影响的不同行的 SQL 查询

标签 php mysql 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>

最佳答案

尝试以完整的方式处理准备好的语句,例如通过使用适当的验证和 exception handling 。只有阅读documentation,您才能实现这一目标。您正在使用的每个 PHP 函数。特别是有关数据库访问操作的内容,尤其是文档的“返回值”部分。

您只需要一张带有四个提交按钮的表单。每个按钮包含相应的Id值(value)。所有按钮都具有相同的名称(我选择“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>

关于php - 如何根据单击的按钮重用受影响的不同行的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46247615/

相关文章:

php - 如何在PHP中旋转图像?

mysql - 将字段排序规则更改为 utf-8 会导致重复键错误

sql - 在 1 个查询中获得问题和答案

c# - LINQ 查询 AND & OR

php - 如何编辑我的查询,每个产品仅显示一张图片?

php - Mysql Query在PhpMyadmin中有效,但在php中连接时无效

php - 在 PHP 中多次调用 MySQL 存储例程

mysql - 从数据库中删除回车符

mysql - 记录特定表的 mysql 更新查询

php - 使用 LEFT OUTER JOIN 连接多个表