Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.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过滤MySQL数据库的问题_Php_Mysql - Fatal编程技术网

使用内部搜索PHP过滤MySQL数据库的问题

使用内部搜索PHP过滤MySQL数据库的问题,php,mysql,Php,Mysql,我正在尝试使用此搜索框过滤我创建的一个小虚拟数据库,但当我尝试在网站上的搜索框中搜索时,它会给我一个错误: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= ''' at line 1 我感觉MySQL查询工作不正常,因为“SELECT*FROM Table WHERE C

我正在尝试使用此搜索框过滤我创建的一个小虚拟数据库,但当我尝试在网站上的搜索框中搜索时,它会给我一个错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= ''' at line 1
我感觉MySQL查询工作不正常,因为“SELECT*FROM Table WHERE City=what is searched”听起来不对

<?php

mysql_connect("***","******","*****") or die(mysql_error());
mysql_select_db("*****");

$sql = "SELECT * FROM Table";

if (isset($_POST['search'])) {

    $search_term = mysql_real_escape_string($POST['search_box']);

    $sql .= "WHERE City = '{$search_term}'";


}




$query = mysql_query($sql) or die(mysql_error());


?>
<form name='search' method="POST" action="test.php" >

Search: <input type="text" name="search_box" value="">
<input type="Submit" name="search" value="Search the table...">

</form>

<table width="70%">
<tr>
    <td><strong>City</strong></td>
    <td><strong>Country</strong></td>
    <td><strong>Climate</strong></td>
    <td><strong>Company</strong></td>
    <td><strong>Activities</strong></td>
    <td><strong>Continent</strong></td>
    <td><strong>Terrain</strong></td>
</tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
    <td><?php echo $row['City']; ?></td>
    <td><?php echo $row['Country']; ?></td>
    <td><?php echo $row['Climate']; ?></td>
    <td><?php echo $row['Company']; ?></td>
    <td><?php echo $row['Activities']; ?></td>
    <td><?php echo $row['Continent']; ?></td>
    <td><?php echo $row['Terrain']; ?></td>
</tr>
<?php } ?>
</table>

谢谢。

你的打字错误。这:

$POST['search_box']
应该是这样的:

$_POST['search_box']
这意味着您正在创建一个空变量(
$search\u term
)。如果在上有错误报告,您会看到这一点。下次开发时,请将其放在脚本的顶部:

ini_set('display_errors', 1);
error_reporting(-1); // or you could use E_ALL
最后但并非最不重要的一点是,您需要在
WHERE
前面留出一个空格:

$sql .= " WHERE City = '{$search_term}'";

注释

  • 您还需要运行查询(即,
    mysql\u query()
  • 你最好还是学习一下/mysql.*已经被弃用了
关于搜索查询,还有一件事需要注意。您只能找到与提交的搜索查询完全匹配的匹配项。您最好坚持最佳实践,并使用以下工具进行搜索:


确保$\u POST变量的命名正确,并且WHERE子句前面有一个空格。试试这个:

$sql = "SELECT * FROM Table";

if (isset($_POST['search_box'])) {

    $search_term = mysql_real_escape_string($POST['search_box']);

    $sql .= " WHERE City = '{$search_term}'";


}

我现在似乎遇到了一个错误:“您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以了解在第1行“WHERE City='Rome''附近使用的正确语法”我认为这与数据库的设置方式有关,但我不知道我做错了什么。感谢您的帮助,下次我将使用显示错误。我现在得到了一个不同的错误,但我相信这与我设置数据库的方式有关。目前还不太确定。另外,您应该记得升级到mysqli或PDO。当您连接查询时,您忽略了
其中
$sql = "SELECT * FROM Table";

if (isset($_POST['search_box'])) {

    $search_term = mysql_real_escape_string($POST['search_box']);

    $sql .= " WHERE City = '{$search_term}'";


}