PHP mySQL搜索不工作 >项目ID、名称、描述。 我一直得到“没有搜索结果”输出的0个结果。你知道问题出在哪里吗

PHP mySQL搜索不工作 >项目ID、名称、描述。 我一直得到“没有搜索结果”输出的0个结果。你知道问题出在哪里吗,php,html,mysql,search,search-engine,Php,Html,Mysql,Search,Search Engine,您的代码存在一些问题 首先,您省略了变量的美元符号,从技术上讲,您将搜索“searchq”或“serachq”文本字符串。“serachq”是一个打字错误,如下所述 <?php $username = "root"; $password = ""; $hostname = "localhost"; $db_handle = mysql_connect($hostname, $username, $password) or die ("Could n

您的代码存在一些问题

首先,您省略了变量的美元符号,从技术上讲,您将搜索“searchq”或“serachq”文本字符串。“serachq”是一个打字错误,如下所述

<?php
     $username = "root";
     $password = "";
     $hostname = "localhost";

     $db_handle = mysql_connect($hostname, $username, $password) or die ("Could not connect to database");

     $selected= mysql_select_db("login", $db_handle);

     $output='';

     if(isset($_POST['search'])){
         $searchq = $_POST['search'];

         $query= "SELECT * FROM PHP_Item WHERE Name LIKE '%searchq%' OR Description LIKE '%serachq%'" or die ("could not search");
         $result= mysql_query($query);
         $count= mysql_num_rows($result);

         echo $count;

         if($count <1){
             $output = 'there were no search results';
         }else{
             while($row = mysql_fetch_array($query)){
                 $mName = $row['Name'];
                 $price = $row['Price'];
                 $id= $row['ItemID'];

                 $output .= '<div>'.$mName.' '.$price.'</div>';
             }
         }
     }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> --><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> --><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> --><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
<html>
    <head>
        <title>Movie Search-Search for a movie</title>
    </head>
    <body>
        <form action="search.php" method="POST">
              <input type="text" name="search" placeholder="Find movies..."/>
              <input type="submit" value="Search movies"/>
        </form>

            <?php print("$output");?>
    </body>
</html>
根据
$searchq=$\u POST['search']

另外,您还在
中的
serachq
一词中输入了一个拼写错误,如“%serachq%”

重写:

'%searchq%' OR Description LIKE '%serachq%'
  • 检查错误会发现这些错误
您的
或死亡(“无法搜索”)或die(mysql\u error())
添加到
mysql\u query()
的说明

那么这一行

'%$searchq%' OR Description LIKE '%$searchq%'
应该在
$result=mysql\u查询($query)中引用
$result
和非
$query

while($row = mysql_fetch_array($query))
添加到文件的顶部,这将有助于查找错误

while($row = mysql_fetch_array($result))
  • 这将打破你的页面

  • 只需将整个块更改为


您当前的代码对用户开放。使用,或者,它们更安全


条件语句选项:

这一行:
if(isset($\u POST['search'])
也可以更改为

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> --><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> --><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> --><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
以确保输入不为空

或者简单地说:

if( 
    isset($_POST['search']) && 
    !empty($_POST['search']) 
    )

在查询中,尝试在searchq之前添加$,否则它将被视为字符串''searchq'ie-请将“%searchq%”更改为“%$searchq%”。它们不再得到维护,而是在使用。请改为学习,并使用。谢谢您的编辑@sєєєm sєm,但我需要在我的答案底部进行额外编辑,但我保留了您的编辑;再次感谢。谢谢你的帮助,我才刚刚开始学习PHP,这就是为什么我会犯这些愚蠢的错误。下一次我将在检查代码错误时更加小心。
if( 
    isset($_POST['search']) && 
    !empty($_POST['search']) 
    )
if( !empty($_POST['search']) )