【问题标题】:Showing only the search results without displaying the whole table只显示搜索结果而不显示整个表格
【发布时间】:2020-08-10 01:30:03
【问题描述】:

我希望网页在单击搜索按钮后显示结果。目前,整个表格在页面加载时显示。 如果我从“Select * FROM users”中更改变量 $sql 的值,则搜索查询不再起作用

我正在使用 XAMPP,网页是 php 文件,数据库是 Mariadb

用户表

+--------+-------------+---------------+--------------------------+--------+
|    ID  |  firstname  |  lastname     |    address               |  count |
|        |             |               |                          |        |
+--------------------------------------------------------------------------+
|     1  |    john     |    doe        |james street, idaho, usa  |  2     |                    
|        |             |               |                          |        |
+--------------------------------------------------------------------------+
|     2  |    cindy    |   smith       |rollingwood av,lyn, canada| 1      |
|        |             |               |                          |        |
+--------------------------------------------------------------------------+
|     3  |    rita     |   chatsworth  |arajo ct, alameda, cali   | 0      |
|        |             |               |                          |        |
+--------------------------------------------------------------------------+
|     4  |    randy    |   plies       |smith spring, lima, (peru)| 1      |                       
|        |             |               |                          |        |
+--------------------------------------------------------------------------+
|     5  |    Matt     |   gwalio      |park lane, (atlanta), usa | 2      |
|        |             |               |                          |        |
+--------------------------------------------------------------------------+

带有搜索栏的网页。

<?php
ini_set('memory_limit', '1042M');
$localhost = "localhost";
$username = "root";
$password = "";
$dbname = "samueldb";
$con = new mysqli($localhost, $username, $password, $dbname);
if( $con->connect_error){
  die('Error: ' . $con->connect_error);
}

$sql = "SELECT * FROM users";

if( isset($_GET['btn']) ){
  $name = mysqli_real_escape_string($con, htmlspecialchars($_GET['search']));
   $sql =  "SELECT * FROM 'users' WHERE 'firstname' ='$name'";
}
$result = $con->query($sql);

?>
<!DOCTYPE html>
<html>
<head>
     <title>Find my search results</title>
<link href="css/templatemo-style.css" rel="stylesheet" />
 <link href="css/font-awesome.min.css" rel="stylesheet" />
<link href="css/font-awesome.css" rel="stylesheet" />
<link href="css/bootstrap.css" rel="stylesheet" />
 <link href="css/search-style.css" rel="stylesheet" />

</head>

<body>

  <h2>List of students</h2>

     <div class="search">

    <input type="text" class="searchTerm" placeholder="What are you looking for?">
    <button type="search" class="searchButton">
      <i class="fa fa-search"></i>
   </button>
 </div>
</div>
           <table class="table table-striped table-dark">
           <tr>
           <th>ID</th>
           <th>First_Name</th>
           <th>Address</th>
           <th>Count</th>
           </tr>

           <?php

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

               ?>
               <tr>
               <td><?php echo $row['ID']; ?></td>
               <td><?php echo $row['firstname']; ?></td>
               <td><?php echo $row['address']; ?></td>
               <td><?php echo $row['count']; ?></td>
               </tr>

               <?php
              }
               ?>
           </table>
           </div>


</body>
</html>

【问题讨论】:

标签: php html sql


【解决方案1】:

您的代码存在一些问题。

  1. 您的form。您忘记将输入包装在 form 标记中。您还应该在输入中添加name="search",而button 上的type="search" 没有意义。将您的表单更改为
<form>
    <input type="search" name="search" class="searchTerm" placeholder="What are you looking for?">
    <button type="submit" name="btn" class="searchButton">
        <i class="fa fa-search"></i>
    </button>
</form>

name 属性应该与您的 $_GET[name] 匹配

  1. 您的搜索查询。您应该只在值上加上单引号,而不是在列上。将您的查询更改为
$sql = "SELECT * FROM users WHERE firstname = '$name'";
  1. 最重要的是,您没有使用准备好的语句和参数化查询。您所做的事情容易受到 SQL 注入攻击。阅读更多here尽快。

【讨论】:

  • 我插入了您的代码,打开页面时仍然显示搜索结果。
  • 可能是因为你的 url 上还有 btn 参数。如果存在,将使用我们的搜索查询。
  • 在点击搜索按钮之前,url上没有按钮参数。
  • 我注意到我有以下行可能会导致表格在页面加载时显示: $sql = "SELECT * FROM 'users' " 但是如果我删除此行然后我收到一个错误:Fatal error: Uncaught Error: Call to a member function fetch_assoc() on bool in C:\xampp\htdocs\opensourcetest.php:58 Stack trace: #0 {main} thrown in C:\xampp\htdocs\opensourcetest.php on line 58
猜你喜欢
  • 2023-04-01
  • 2014-01-20
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多