【问题标题】:php/mysql search function issuephp/mysql 搜索功能问题
【发布时间】:2015-12-22 15:12:09
【问题描述】:

我觉得我只需要另一双眼睛。数据库中当然有要搜索的内容,但没有显示任何内容。语法或逻辑是否有问题。这一切都在一个文件中 index.php

    <form action = "index.php" method = "post">
 Search: <input type="text" name="value" placeholder="Is it part of the FWO?"></input>
  <input type=submit name = "search" value="Search">
    </form> 

  <a href="LINKY">New Entry</a>
  <br>
  <p>Search Results</p>
  <hr /> 


  <?php
       error_reporting(E_ALL);
       $title = $_POST['value'];
       echo "You have searched: " .$title;
       echo "<br>";

       $con = mysql_connect("localhost", "user", "pass") or die ('Could not connect, this is the error:  ' . mysql_error());
       mysql_select_db("db") or die ('Sorry could not access database at this time.  This is the error:  ' . mysql_error());   

       $clean = msql_real_escape_string($_GET['value']);
       echo "Another test ". $clean;
       $run = mysql_query("SELECT * FROM db WHERE name = '$clean'") or die(mysql_error());

       if(mysql_num_rows($run) >= 1){
        echo "found entry";
        while($i = mysql_fetch_array($run)){
            echo $i['creator'];
        }

       }

       else {
        echo "No entries found";
       }


       mysql_close($con);
?>



  </body> 
</html> 

【问题讨论】:

  • 你的值是 POST 还是 GET 值,因为你同时使用了它们
  • 代替$_GET,你应该使用$_POST
  • 改变这个 $clean = msql_real_escape_string($_GET['value']);到 $clean = msql_real_escape_string($title);
  • 您是否创建了msql_real_escape_string?你在检查你的错误日志吗?当您说nothing is displayed 时,您的意思是您得到的是空白页还是没有搜索结果?

标签: php mysql database function search


【解决方案1】:

您的表单正在使用 post 方法,您正在尝试通过 $_GET 获取值

而不是这个:

$clean = msql_real_escape_string($_GET['value']);

使用这个:

$clean = msql_real_escape_string($_POST['value']);

或者

$clean = msql_real_escape_string($title);

【讨论】:

    【解决方案2】:

    要在 mysql 中搜索,您应该使用 LIKE。如果你想搜索字符串中的任何地方,你应该用 % 封装。例如:

       $run = mysql_query("SELECT * FROM db WHERE name LIKE '%$clean%'") or die(mysql_error());
    

    更多信息:http://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 2015-04-11
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      相关资源
      最近更新 更多