添加到相同的php页面并等待用户输入

hak*_*ata 2 html php mysql pdo

我试图从用户那里获得输入,然后当他按下提交时,它将在数据库中进行查询并将结果返回到表中.然而,我的代码似乎没有等待,因为它会自动执行我保存查询结果的var为false的场景.我试图在同一页面上执行此操作,因为如果我将数据发送到另一个页面,它可以工作,但我想最小化我正在使用的文件数量.

这是我的代码:

<!doctype html>

<html>
    <head>
        <meta charset="UTF-8">
        <title>View a Faculty Member</title>
        <style type="text/css">
            .block {
                display:block;
                margin-top:3px;
            }
        </style>
    </head>

    <body>
        <h1>View a faculty member:</h1>

        <form action="" method="post">
            <label class="block">Please Enter ID: <input type="text" placeholder="ie. 12345" maxlength="30" size="30" name="id"></label>

            <input class="block" type="submit" name="submit" value="Get Faculty Member">
        </form>

        <hr/>

        <?php

        // 1- get the id
        // 2- construct query in string
        // 3- connect to db
        // 4- execute the query
        // 5- put the result in a var
        // 6- get the information from the var into a table

        $id = $_POST["id"]; // step 1
        $query = "SELECT * FROM FM WHERE id=\"$id\""; // step 2

        try {
            $db = new PDO("mysql:dbname=project;host=localhost", "khaled", ""); // step 3
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $row = $db->query($query)->fetch(); // step 4 & step 5

            if ($row == FALSE) {
                // id doesnt exist since the query failed
            ?>

            <h3>The ID you entered does not exist.</h3>
            <h4>Please try again.</h4>

            <?
            } else {
                // step 6
                displayTable($row);
            }
        } catch (PDOException $ex) {
        ?>

            <h3 style="color:red;">An error occured.</h3>
            <p>An error occured during the connection to the database.</p>
            <p>Exact error message: <?= $ex->getMessage() ?></p>

        <?php
        }

        function displayTable ($row) {
        ?>

            <table border="1">
                <tr>
                    <th>ID</th>
                    <th>Last Name</th>
                    <th>First Name</th>
                    <th>Office</th>
                    <th>Extension</th>
                    <th>Home Phone</th>
                    <th>Mobile Phone</th>
                    <th>Address</th>
                    <th>Email</th>
                    <th>Starting Year</th>
                    <th>Termination Year</th>
                    <th>Latest Degree</th>
                    <th>Obtained From</th>
                    <th>Degree Year</th>
                    <th>Research Interest</th>
                </tr>
                <tr>
                    <td><?= $row["id"] ?></td>
                    <td><?= $row["lastName"] ?></td>
                    <td><?= $row["firstName"] ?></td>
                    <td><?= $row["office"] ?></td>
                    <td><?= $row["extension"] ?></td>
                    <td><?= $row["homePhone"] ?></td>
                    <td><?= $row["mobilePhone"] ?></td>
                    <td><?= $row["address"] ?></td>
                    <td><?= $row["email"] ?></td>
                    <td><?= $row["startingYear"] ?></td>
                    <td><?= $row["terminationYear"] ?></td>
                    <td><?= $row["latestDegree"] ?></td>
                    <td><?= $row["obtainedFrom"] ?></td>
                    <td><?= $row["degreeYear"] ?></td>
                    <td><?= $row["researchInterest"] ?></td>
                </tr>
            </table>

        <?php
        }       
        ?>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

如何让我的代码等待用户输入输入然后检查它是否是错误的查询?

jde*_*ere 7

你可以像这样轻松检查: if(isset($_POST['submit'])){...}.在那,如果你应该把查询和东西的代码.