[英]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.