简体   繁体   English

PHP页面不显示数据表

[英]PHP Page Does not Display Data Table

I have a search box that displays the search results in a table. 我有一个搜索框,在表格中显示搜索结果。 The search box uses a simple search query to get the data from a database. 搜索框使用简单的搜索查询从数据库中获取数据。

below is the code for the search box 以下是搜索框的代码

 <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

The PHP: 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');
?>

When I run the code the page displayed is empty. 当我运行代码时,显示的页面为空。

Check out the following: 查看以下内容:

<form id="search-form" mmethod="post" action="search.php">

Should be: 应该:

<form id="search-form" method="post" action="search.php">

You need to escape it. 您需要逃脱它。 The way it is now you are wide open to SQL-injection 现在的方式是您对SQL注入持开放态度

$searchword = $_POST['seacher']; 

So something like below. 所以像下面这样。 Also note the error : seacher / searcher in the $_POST 另请注意错误: $_POST中的seacher / searcher

  $searchword = $con->real_escape_string( $_POST['searcher'] ); 

Put (``) backticks around table and column names to prevent "mysql reserved word error" 在表名和列名前后加上(``)反引号,以防止出现“ MySQL保留字错误”

// Retrieve data from database 
$sql="SELECT * FROM `$tbl_name` WHERE `first_name` = '$searchword' OR `last_name` = '$searchword' "; 

Remove the first fetch because it will interfere with the other 删除第一个访存,因为它会干扰另一个

$rows = $result->fetch_assoc();  

Just keep the one just before your table 只要把那个放在桌子前

while($rows = $result->fetch_assoc()){

Note the error in your table 注意表中的错误

<td class="center"><?php echo $rows['first_name']; ?></td> <!-- ['fisrt_name'] -->

For your 为您

if($result->num_rows > 0){

You could add the following: 您可以添加以下内容:

} else {
  echo 'Nothing found'; 
}

In you HTML, Input must has a name that you are trying to post. 在您的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>

and in your PHP make sure you are posting; 并在您的PHP中确保您正在发布;

if(isset($_POST['submit']) && $_POST['submit']=="Find"){

AND try like this; 并尝试这样;

$sql="SELECT * FROM '$tbl_name' WHERE first_name='$searchword' OR last_name='$searchword' ";

OR 要么

$sql="SELECT * FROM $tbl_name WHERE first_name='$searchword' OR last_name='$searchword' ";

AND

// 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
?>

Because later you are running a loop here 因为稍后您在这里运行循环

<?php
if($result->num_rows > 0){
// output data of each row
while($rows = $result->fetch_assoc()){ ?>

sorry dude, but there are a couple of issues here 抱歉,老兄,但是这里有几个问题

firstly fix your form: method="post" - not mmethod; 首先修复您的表单:method =“ post”-不是方法; type="text" - not type="search"; type =“ text”-不​​是type =“ search”; </form> - not </form </ 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>

secondly change: 其次改变:

if (isset($_POST['searcher'])) { // changed from submit

thirdly handle any errors in your query: 第三,处理查询中的任何错误:

if (($result = $con->query($sql)) === false) {
  die("error: ".$con->error); // todo: improve error handling
}

fourthly remove this line: 第四,删除此行:

$rows = $result->fetch_assoc();

finally, for my sanity only, please remove this line (you really don't need it): 最后,仅出于我的理智,请删除此行(您确实不需要):

if($result->num_rows > 0){

and it's matching: 和它匹配:

}

and don't even get me started on escaping user input in case of SQL injection! 而且甚至在SQL注入的情况下也不要让我开始转义用户输入!

good luck, I hope you get this running 祝你好运,我希望你能成功

您在代码中两次使用$result->fetch_assoc()这行,因此您的查询只有1个结果,然后第一个陈述出现1个结果,然后当您尝试第二次在表的html之前使用时,您什么也没得到。

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

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