PHP页面不显示数据表

时间:2015-07-18 15:44:32

标签: php html

我有一个搜索框,可以在表格中显示搜索结果。搜索框使用简单的搜索查询从数据库中获取数据。

下面的

是搜索框的代码

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

当我运行代码时,显示的页面为空。

4 个答案:

答案 0 :(得分:1)

查看以下内容:

<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'; 
}

答案 1 :(得分:0)

在HTML中,输入必须具有您要发布的名称。

<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' ";

OR

$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()){ ?>

答案 2 :(得分:0)

抱歉,老兄,但这里有几个问题

首先修复您的表单: 方法=&#34;后&#34; - 不是mmethod;类型=&#34;文本&#34; - 不是类型=&#34;搜索&#34 ;; &LT; /形式&GT; - 不是&lt; / 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注入的情况下逃避用户输入!

祝你好运,我希望你能跑步

答案 3 :(得分:0)

您在代码中使用了stack这一行两次,因此您的查询只有1个结果,然后1个结果出现在第一个语句中,然后当您尝试第二次在表格的html之前使用时,您什么也得不到。< / p>