douchun1948 2015-07-18 15:44
浏览 27
已采纳

PHP页面不显示数据表

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:

$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.

  • 写回答

4条回答 默认 最新

  • duan198299 2015-07-18 16:42
    关注

    sorry dude, but there are a couple of issues here

    firstly fix your form: method="post" - not mmethod; type="text" - not type="search"; </form> - not </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!

    good luck, I hope you get this running

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度