在表中显示SQL搜索表单结果

时间:2017-11-19 23:44:19

标签: php html mysql

我正试图通过'SUBMIT'表单从'SQL'数据库中提取信息。连接是我假设的,因为它与条目的代码相同,并且工作得很好。我可以将结果显示为项目块,但不将其集成到表格中。

现在代码是,它只返回一个空白页......

我在几年前找到了许多例子,但事情发生了变化......

以下是PHP页面上应该在表格中显示结果的代码。

我已经尝试过不同的方法将结果集成到表中,然后再来这里寻求帮助,但都返回相同的结果。我对此很新。

 <?php

$servername = "localhost";
$username = "root";
$password = "Rmvs03ff";
$dbname = "EmployeeListing";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if (isset($_POST['submit-search'])) {
    $search = mysqli_real_escape_string($conn, $_POST['search']);
    $sql = "SELECT * FROM BasicEmployee WHERE EmployeeID LIKE '%$search%' OR FirstName LIKE '%$search%' OR LastName LIKE '%$search%' OR DoB LIKE '%$search%'";
    $result = mysqli_query($conn, $sql);
    $queryResult = mysqli_num_rows($result);

    echo "<br/>There are " .$queryResult. " matches found";

    echo "<table>
            <tr>
            <th>Employee I.D.</th>
            <th>Sex</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Date of Birth</th>
            </tr>";

    if ($queryResult > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo    "<tr>"
            echo    "<td><strong>
                    ".$row['EmployeeID']."
                    </strong></td>"
            echo    "<td>
                    ".$row['Prefix']."
                    </td>"
            echo    "<td><p>
                    ".$row['FirstName']."
                    </p></td>"
            echo    "<td><p>
                    ".$row['LastName']."
                    </p></td>"
            echo    "<td><p>
                    ".$row['DoB']."
                    </p></td>";
            echo "</table>";
        }

    } else {
        echo "<br/>No results found";
    }

}

echo "<br/>Approved";

$conn->close();

?>
   </body>
   </html>

我确实有开头的html,当然是一个机构,不想用这个阻挡空间......

1 个答案:

答案 0 :(得分:0)

你需要回复整个表格一次:

<?php

$servername = "localhost";
$username = "root";
$password = "Rmvs03ff";
$dbname = "EmployeeListing";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if (isset($_POST['submit-search'])) {
    $search = mysqli_real_escape_string($conn, $_POST['search']);
    $sql = "SELECT * FROM BasicEmployee WHERE EmployeeID LIKE '%$search%' OR FirstName LIKE '%$search%' OR LastName LIKE '%$search%' OR DoB LIKE '%$search%'";
    $result = mysqli_query($conn, $sql);
    $queryResult = mysqli_num_rows($result);

    echo "<br/>There are " .$queryResult. " matches found";

    $table = "<table>
            <tr>
            <th>Employee I.D.</th>
            <th>Sex</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Date of Birth</th>
            </tr>";

    if ($queryResult > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $table .= "<tr>";
            $table .= "<td><strong>
                    ".$row['EmployeeID']."
                    </strong></td>";
            $table .= "<td>
                    ".$row['Prefix']."
                    </td>";
            $table .= "<td><p>
                    ".$row['FirstName']."
                    </p></td>";
            $table .= "<td><p>
                    ".$row['LastName']."
                    </p></td>";
            $table .= "<td><p>
                    ".$row['DoB']."
                    </p></td>";
            $table .= "</table>";
        }

    } else {
        echo "<br/>No results found";
    }

}
echo $table;


echo "<br/>Approved";

$conn->close();

?>
   </body>
   </html>
相关问题