[英]PHP Page Does not Display Data Table
我有一个搜索框,在表格中显示搜索结果。 搜索框使用简单的搜索查询从数据库中获取数据。
以下是搜索框的代码
<form id="search-form" mmethod="post" action="search.php">
<input name="searcher" id="search-bar" type="search" placeholder="Type to Search">
<input id="search-button" type="submit" value="Find">
</form
PHP:
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="datacentre"; // Database name
$tbl_name="data_centre_users"; // Table name
$server_name="localhost";
if(isset($_POST['submit'])) {
$searchword = $_POST['seacher'];
// Create connection
$con = new mysqli($server_name, $username, $password, $db_name , 3306);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE first_name='$searchword' OR last_name='$searchword' ";
$result = $con->query($sql);
$rows = $result->fetch_assoc();
?>
<section id="sidebar">
</section>
<section id="content">
<div id="scroll-table">
<table >
<caption>
Search Results
</caption>
<tr>
<th class="center"><strong>ID</strong></th>
<th class="center"><strong>FirstName</strong></th>
<th class="center"><strong>Lastname</strong></th>
<th class="center"><strong>Request</strong></th>
<th class="center"><strong>Purpose</strong></th>
<th class="center"><strong>Description</strong></th>
<th class="center"><strong>Booking Time</strong></th>
<th class="center"><strong>Access Time</strong></th>
<th class="center"><strong>Exit Time</strong></th>
<th class="center"><strong>Approved</strong></th>
<th class="center"><strong>Approved By</strong></th>
<th class="center"><strong>Update</strong></th>
</tr>
<?php
if($result->num_rows > 0){
// output data of each row
while($rows = $result->fetch_assoc()){ ?>
<tr>
<td class="center"><?php echo $rows['id']; ?></td>
<td class="center"><?php echo $rows['fisrt_name']; ?></td>
<td class="center"><?php echo $rows['last_name']; ?></td>
<td class="center"><?php echo $rows['request']; ?></td>
<td class="center"><?php echo $rows['purpose']; ?></td>
<td class="center"><?php echo $rows['description']; ?></td>
<td class="center"><?php echo $rows['booking_time']; ?></td>
<td class="center"><?php echo $rows['access_time']; ?></td>
<td class="center"><?php echo $rows['exit_time']; ?></td>
<td class="center"><?php echo $rows['approved']; ?></td>
<td class="center"><?php echo $rows['approved_by']; ?></td>
<td class="center" ><a href="update.php?id=<?php echo $rows['id']; ?>">update</a></td>
</tr>
<?php
}
}
?>
</table>
</div>
</section>
<
<aside></aside>
<?php
$con->close();
}
include('footer.php');
?>
当我运行代码时,显示的页面为空。
查看以下内容:
<form id="search-form" mmethod="post" action="search.php">
应该:
<form id="search-form" method="post" action="search.php">
您需要逃脱它。 现在的方式是您对SQL注入持开放态度
$searchword = $_POST['seacher'];
所以像下面这样。 另请注意错误: $_POST
中的seacher / searcher
$searchword = $con->real_escape_string( $_POST['searcher'] );
在表名和列名前后加上(``)反引号,以防止出现“ MySQL保留字错误”
// Retrieve data from database
$sql="SELECT * FROM `$tbl_name` WHERE `first_name` = '$searchword' OR `last_name` = '$searchword' ";
删除第一个访存,因为它会干扰另一个
$rows = $result->fetch_assoc();
只要把那个放在桌子前
while($rows = $result->fetch_assoc()){
注意表中的错误
<td class="center"><?php echo $rows['first_name']; ?></td> <!-- ['fisrt_name'] -->
为您
if($result->num_rows > 0){
您可以添加以下内容:
} else {
echo 'Nothing found';
}
在您的HTML中,Input必须具有您要发布的名称。
<form id="search-form" method="post" action="search.php"> //spelling correction as you have type mistake mmethod
<input name="searcher" id="search-bar" type="text" placeholder="Type to Search">
<input id="search-button" type="submit" name="submit" value="Find">
</form>
并在您的PHP中确保您正在发布;
if(isset($_POST['submit']) && $_POST['submit']=="Find"){
并尝试这样;
$sql="SELECT * FROM '$tbl_name' WHERE first_name='$searchword' OR last_name='$searchword' ";
要么
$sql="SELECT * FROM $tbl_name WHERE first_name='$searchword' OR last_name='$searchword' ";
和
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE first_name='$searchword' OR last_name='$searchword' ";
$result = $con->query($sql);
$rows = $result->fetch_assoc(); //Remove this you don't need it
?>
因为稍后您在这里运行循环
<?php
if($result->num_rows > 0){
// output data of each row
while($rows = $result->fetch_assoc()){ ?>
抱歉,老兄,但是这里有几个问题
首先修复您的表单:method =“ post”-不是方法; type =“ text”-不是type =“ search”; </ form>-不是</ form
<form id="search-form" method="post" action="search.php">
<input name="searcher" id="search-bar" type="text" placeholder="Type to Search">
<input id="search-button" type="submit" value="Find">
</form>
其次改变:
if (isset($_POST['searcher'])) { // changed from submit
第三,处理查询中的任何错误:
if (($result = $con->query($sql)) === false) {
die("error: ".$con->error); // todo: improve error handling
}
第四,删除此行:
$rows = $result->fetch_assoc();
最后,仅出于我的理智,请删除此行(您确实不需要):
if($result->num_rows > 0){
和它匹配:
}
而且甚至在SQL注入的情况下也不要让我开始转义用户输入!
祝你好运,我希望你能成功
您在代码中两次使用$result->fetch_assoc()
这行,因此您的查询只有1个结果,然后第一个陈述出现1个结果,然后当您尝试第二次在表的html之前使用时,您什么也没得到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.