繁体   English   中英

PHP / MYSQL搜索表单显示空结果

[英]PHP /MYSQL Search Form Displaying Empty Results

拜托,我的代码有什么问题,我试图从数据库中获取搜索数据,如下所示

学生数据如下

注册编号:全名:系:课程:级别:组。

在HTML搜索页面上

<html> 
<h2> Enter your matric number to connect to others studying your course </h2>
<form action="demo.php" method="post">
<b> ReG </b><input type="text" Name="find">
<input type="submit"  value="Submit" />

</form>
</html>

PHP端

<table border="1">
<tr>
<th>Student Full Name</th>
<th> Faculty</th>
<th> Program</th>
<th> Entry Year</th>
<th> Study Group</th>
<th> Group Members Contact</th>
<th> Group Leader Contacts</th>
</tr>



<?php 

$conn=mysqli_connect("localhost", "root", "", "student");
// Check connection
if($conn=== false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

 $q = $_POST['find'];


if ($q == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
$sql = "SELECT * FROM study_circle WHERE matric LIKE $q ";
$result = mysqli_query($conn, $sql);

if ($result)
 {
  while($row = mysql_fetch_array($result))
 {





        echo "<tr>";
        echo  "<td>";

    echo  $row['full_name'];
    echo  "</td>";
    echo  "<td>";
    echo  $row['faculty'];
    echo "</td>";
    echo  "<td>";
    echo  $row['program'];
    echo  "</td>";
    echo  "<td>";
    echo  $row['entry_year'];
    echo "</td>";
    echo  "<td>";
    echo  $row['study_group'];
    echo  "</td>";
    echo  "<td>";
    echo  $row['group_members'];
    echo  "</td>";
    echo "<td>";
    echo  $row['group_leader'];
    echo  "</td>";
    echo  "</tr>";
    echo  "<br/>";




    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?>

每次尝试时,即使填充了数据库,它也会带来Empty结果

现在,它在我的Localhost系统上,请任何有经验的人提供帮助,这对Programming来说是新手。 我将很乐意解决此问题

您的代码在这里误解了false值的含义:

if ($result)
{
    //...
} else {
    echo "0 results";
}

$result中的false值并不表示搜索未找到任何内容,而是表示查询失败并显示error 要获取错误,请使用mysqli_error

if ($result)
{
    //...
} else {
    echo "There was an error: " . mysqli_error($conn);
}

在这种情况下,该错误很可能是SQL代码中的语法错误,因为您是直接将用户输入连接到SQL代码。 这也称为SQL注入漏洞 这里有一些很好的信息可帮助您开始进行更正 ,包括这里特别介绍如何使用LIKE运算符使用查询参数 最简单的是,您将要使用带有查询参数的预备语句,而不是像现在那样使用字符串插值。

尝试在搜索字符串的开头和结尾添加通配符:

$sql = "SELECT * FROM study_circle WHERE matric LIKE '%$q%'";

您将需要获取搜索数据的行数。 这将保证记录是否存在,例如。

$rowcount = mysqli_num_rows($result);

然后执行if语句。

参见下面的代码。

<?php 

$conn=mysqli_connect("localhost", "root", "", "student");
// Check connection
if($conn=== false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

 $q = $_POST['find'];


if ($q == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
$sql = "SELECT * FROM study_circle WHERE matric LIKE $q ";
$result = mysqli_query($conn, $sql);
$rowcount = mysqli_num_rows($result);

if( $rowcount ){

$row = mysql_fetch_array($result);





        echo "<tr>";
        echo  "<td>";

    echo  $row['full_name'];
    echo  "</td>";
    echo  "<td>";
    echo  $row['faculty'];
    echo "</td>";
    echo  "<td>";
    echo  $row['program'];
    echo  "</td>";
    echo  "<td>";
    echo  $row['entry_year'];
    echo "</td>";
    echo  "<td>";
    echo  $row['study_group'];
    echo  "</td>";
    echo  "<td>";
    echo  $row['group_members'];
    echo  "</td>";
    echo "<td>";
    echo  $row['group_leader'];
    echo  "</td>";
    echo  "</tr>";
    echo  "<br/>";


    exit;
}else{
 echo "0 results";

}
}


mysqli_close($conn);
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM