当我搜索数据库而不是获取结果时,出现错误500

时间:2019-02-09 05:46:22

标签: php html mysql

我有一个网站,该网站应该在数据库中搜索特定的ID,但单击提交按钮时出现错误500

我正在运行php7和最新的mysql,我尝试删除不需要的代码,我尝试更改我用来从数据库获取信息的方法,没有运气

“ dbh.inc.php”

<?php
$servername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "chromebook";

$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);

if (!$conn) {
 die("Connection Failed: ".mysqli_connect_error());
}

“ search.inc.php(目标页面,显示结果)”

<!DOCTYPE html>
<?php
require "dbh.inc.php";
?>
<html>
<head>
<title>Searching For Data</title>
</head>
<body>
<?php

echo "<h2>Search Results</h2>
if(isset($_POST['search'])) {
$search = $_POST['search'];

if ($search == "") {
echo "<p>You Didn't Submit Data! Try Again!</p><a 
href="myurlusuallyisherebutitsforaimportantthingandidontwantusersgoingtoit"</a>";
exit;
}

$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);

$result = mysqli_query("SELECT * FROM chrome WHERE Teacher LIKE '%search%'");

echo "<table class="table" border='1'>
<tr>
<th>Room Number</th>
<th>Teacher</th>
<th>Front Barcode</th>
<th>Back Barcode</th>
</tr>";

while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['RoomNum'] . "</td>";
echo "<td>" . $row['Teacher'] . "</td>";
echo "<td>" . $row['FrontId'] . "</td>";
echo "<td>" . $row['Barcode'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($conn);
?>
</body>
</html>

我希望页面加载数据库要求的结果,相反,我会从浏览器中收到“ http错误500”消息

感谢您的提前帮助

2 个答案:

答案 0 :(得分:1)

public typealias SomeOtherObject 的第一个参数应为mysqli_query

search.inc.php

$conn

答案 1 :(得分:0)

echo "<h2>Search Results</h2>不完整;添加结尾";

if上未封闭的if(isset($_POST['search'])) {语句

echo "<p>You Didn't Submit Data! Try Again!</p><a href="myurlusuallyisherebutitsforaimportantthingandidontwantusersgoingtoit"</a>";echo "<table class="table" border='1'>上的转义双引号

此外,我强烈建议您在代码中添加一些错误检查,以免这500个错误不是一个谜。

这非常适合页面错误报告:

error_reporting(E_ALL); 
ini_set('display_errors', 1);

因此,总的来说,用以下代码替换现有代码:

<html>
<head>
<title>Searching For Data</title>
</head>
<body>
<?php

// added error reporting
error_reporting(E_ALL); 
ini_set('display_errors', 1);

echo "<h2>Search Results</h2>"; //fixed
if(isset($_POST['search'])) {
    $search = $_POST['search'];

    if ($search == "") {
        echo "<p>You Didn't Submit Data! Try Again!</p><a href=\"myurlusuallyisherebutitsforaimportantthingandidontwantusersgoingtoit\"</a>"; // escape quotes
        exit;
    }

    $search = strtoupper($search);
    $search = strip_tags($search);
    $search = trim($search);

    $result = mysqli_query($conn, "SELECT * FROM chrome WHERE Teacher LIKE '%search%'"); // connection param added

    echo "<table class=\"table\" border='1'>  // escape quotes
    <tr>
    <th>Room Number</th>
    <th>Teacher</th>
    <th>Front Barcode</th>
    <th>Back Barcode</th>
    </tr>";

    while($row = mysqli_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" . $row['RoomNum'] . "</td>";
        echo "<td>" . $row['Teacher'] . "</td>";
        echo "<td>" . $row['FrontId'] . "</td>";
        echo "<td>" . $row['Barcode'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";

    mysqli_close($conn);
} // closing bracket from main 'if' statement
?>