【问题标题】:How to display MYSQL data into PHP search box如何在 PHP 搜索框中显示 MYSQL 数据
【发布时间】:2013-03-31 16:10:33
【问题描述】:

您好,我在执行这项任务时遇到了很多问题

我在 xampp 上建立了一个名为 search_test 的数据库,其中包含名字和姓氏作为字段。我已经设置了一个 php 表单,所以当用户输入一个名字说 Andre 时,它​​会返回数据库中的所有 andres。有一个问题它一直告诉我没有搜索结果,即使我知道数据库中有数据这是应该是一个名为 index.php 的 php 页面的代码

    <?php
    mysql_connect("localhost","michael","xcA123sd") or die(mysql_error());
    mysql_select_db("search_test") or die ("could not find db"); 
     $output ='';
    if (isset ($_POST['search']));
        $searchq = $_POST['search'];
    $query = mysql_query("SELECT * FROM members WHERE firstname LIKE '%searchq%'" ) or die("could not search");
    $count = mysql_num_rows($query);
    if($count == 0){
        $output = 'There was no search results !';
        }else{
        while($row = mysql_fetch_array($query)){
        $fname = $row['firstname'];
        $output .='<div> '.$fname.'</div>';
        }

        }
}


?>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>search</title>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="search" placeholder="search for members"/> 
<input type="submit" value=">>"/>
</form>
<?php print("$output);?>
</body
</html> 

例如,我输入 andre 并得到响应

没有搜索结果!

有人可以帮忙

【问题讨论】:

  • 仔细查看您的查询,即如何构建它。
  • SELECT * FROM members WHERE firstname LIKE $searchq 应该可以工作
  • ive 将其更改为 $query = mysql_query("SELECT * FROM members WHERE firstname LIKE $searchq") 或 die("could not search");现在我无法搜索也尝试了 $query = mysql_query("SELECT * FROM members WHERE firstname LIKE '$searchq'") 或 die("could not search");

标签: php mysql


【解决方案1】:

问题出在这行代码

$query = mysql_query("SELECT * FROM members WHERE firstname LIKE '%searchq%'")

变量searchq后面没有$

【讨论】:

    【解决方案2】:

    首先:你想要

    $query = mysql_query("SELECT * FROM members WHERE firstname LIKE '%searchq%'" ) or die("could not search");
    

    成为

    $query = mysql_query("SELECT * FROM members WHERE firstname LIKE '%$searchq%'" ) or die("could not search");
    

    (注意额外的$)。

    也就是说,您有一个很大的 SQL 注入问题:假设,我“正常”运行一次查询:这让我对列有一个概念。现在我发布' UNION ALL SELECT correct_field_num FROM information_schema.TABLES WHERE NAME LIKE '% 作为我的搜索 - 这给了我你的表结构。通过发布' UNION ALL SELECT correct_column_num FROM any_table_name WHERE 'x' LIKE '%,我可以读取任意表格。

    确保您使用一种广为人知的技术从任何用户输入构建安全查询。从已弃用的 mysql_real_escape_string() 到参数化查询,范围很广。

    【讨论】:

      【解决方案3】:
      LIKE '%searchq%'"
      

      如果你需要成为变量,它会搜索像'searchq'这样的字符串,添加相应的美元符号

      【讨论】:

      • 把它改成这样,所以我把它改成了 $query = mysql_query("SELECT * FROM members WHERE firstname LIKE '$searchq'") 或 die("could not search");没有搜索结果
      【解决方案4】:

      试试这个:

      <?php
      mysql_connect("localhost","michael","xcA123sd") or die(mysql_error());
      mysql_select_db("search_test") or die ("could not find db"); 
      $output ='';
      if (isset($_get['search'])){
          $searchq = $_get['search'];
      }
      $query = mysql_query("SELECT * FROM members WHERE firstname LIKE $searchq" ) or die("could not search");
      $count = mysql_num_rows($query);
      if($count == 0){
          $output = 'There was no search results !';
      }else{
          while($row = mysql_fetch_array($query)){
              $fname = $row['firstname'];
              $output .='<div> '.$fname.'</div>';
          }
      }
      ?>
      
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>search</title>
      </head>
      <body>
      <form action="index.php" method="get">
      <input type="text" name="search" placeholder="search for members"/> 
      <input type="submit" value=">>"/>
      </form>
      <?php print("$output);?>
      </body>
      </html>
      

      【讨论】:

      • 注意:未定义变量:第 8 行 G:\xampp\htdocs\search_test\index.php 中的 searchq 无法搜索
      • 这意味着你的 POST 变量没有设置
      【解决方案5】:

      对于您的选择语句,您有:

      $query = mysql_query("SELECT * FROM members WHERE firstname LIKE '%searchq%'" ) or die("could not search");
      

      应该是:

      $query = mysql_query("SELECT * FROM members WHERE firstname LIKE '%".$searchq."'%" ) or die("could not search");
      

      因为您正在搜索名为 searchq 的变量中的内容,而不是实际的字符串 searchq :)

      【讨论】:

      • 这不是同一个查询。您缺少 % 通配符。
      猜你喜欢
      • 2014-02-13
      • 2012-06-25
      • 2012-12-09
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多