Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP搜索中的错误_Php_Mysql - Fatal编程技术网

PHP搜索中的错误

PHP搜索中的错误,php,mysql,Php,Mysql,这是我的PHP代码的“搜索框”,我要包括在我的网站。当我试图通过输入关键字搜索我的数据库时,出现以下错误 注意:第14行C:\xampp\htdocs\abell12\search.php中未定义的索引:searchterm …这是完整的php代码 index.php <!doctype html> <html> <head> <meta charset="utf-8"> <title>Searc

这是我的PHP代码的“搜索框”,我要包括在我的网站。当我试图通过输入关键字搜索我的数据库时,出现以下错误

注意:第14行C:\xampp\htdocs\abell12\search.php中未定义的索引:searchterm

…这是完整的php代码

index.php

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Search (with keywords) Tutorial</title>
    </head>

    <body>
    <form action="search.php" method="POST">
        <input type="text" name="searchterm" placeholder="Search..."><br />
        <input type="submit" value="Search">
    </form>
    </body>
    </html>

搜索(关键字)教程

search.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Search (with keywords) Tutorial</title>
</head>

<body>
<?php

mysql_connect("127.0.0.1","root","root");
mysql_select_db("dummy_info");

$search = mysql_real_escape_string($_POST['searchterm']);

$find_books = mysql_query("SELECT * FROM `listings` WHERE `title` LIKE'%$search%'");
while($row = mysql_fetch_assoc($find_books))
{
    $title = $row['title'];


    echo "$title<br />";

}

?>
</body>
</html>  

搜索(关键字)教程

您应该检查是否首先设置了$\u POST['searchterm']。只有在提交表单时才会设置

也不要使用Mysql函数。他们被弃用了。我会建议您使用,因为它们可以防止SQL注入攻击等

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

    $find_books = mysql_query("SELECT * FROM `listings` WHERE `title` LIKE '%$search%'");

    if(mysql_num_rows($find_books) > 0)
    {
        while($row = mysql_fetch_assoc($find_books))
        {
            $title = $row['title']; 
            echo "$title<br />";
        }
    }
    else
    {
        echo "No Record found";
    }
}
if(isset($\u POST['searchterm']))
{
$search=mysql\u real\u escape\u字符串($\u POST['searchterm']);
$find_books=mysql_query(“从`listings`中选择*WHERE`title`类似“%$search%”);
如果(mysql_num_rows($find_books)>0)
{
而($row=mysql\u fetch\u assoc($find\u books))
{
$title=$row['title'];
回显“$title
”; } } 其他的 { 回显“未找到记录”; } }
这不是一个有效的标题“请帮助”没有告诉我们什么,但“出错”也没有。尝试描述错误。@janDvorak这是错误。。。注意:未定义索引:C:\xampp\htdocs\abell12\search.php中的searchterm在线14@Darren使用isset($_POST['searchterm'])<代码>mysql
已弃用。您应该建议更改为
mysqli\uuu
或PDO。是的,PDO是现代且安全的方式@KanishkDudeja谢谢,它成功了。如果用户输入了数据库中不存在的关键字,您能告诉我如何显示错误消息(如未找到结果)。@Darren:已更改回显“未找到记录”。请检查更新的答案。@KanishkDudeja:非常感谢。它正在工作。!