如何根据单击的按钮重用具有不同行的SQL查询

时间:2017-09-15 21:18:22

标签: 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>

1 个答案:

答案 0 :(得分:1)

尝试以完整的方式处理预准备的陈述,例如通过使用适当的验证和exception handling。只有在您正在阅读正在使用的每个PHP函数的documentation时,才能实现此目的。特别是关于数据库访问操作的那些,尤其是&#34;返回值&#34;部分文件。

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

我还添加了三个<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>