0

我有一个搜索数据库字段“名称”和“地址”的搜索表单,以及一个搜索数据库字段“类型”并在 search_results.php 页面中显示结果的下拉列表。我的基本目标是搜索名称和地址字段,例如“dune”,并获得与其中包含“dune”的任何单词匹配的结果,例如搜索“dune”会返回“binedune”等结果, 'countdune'、'sanddune' 等

此外,如果搜索返回空结果,尤其是在使用下拉列表时,我不希望出现“空子字符串”错误。例如,数据库中“markers”表的“type”列包含字段“New York”、“Munich”、“London”和“Nairobi”。除了“新泽西”、“开罗”等其他城市之外,这与下拉列表中显示的列表选项相同。当我选择“开罗”等城市时,它会在数据库中进行搜索但没有找到Cairo 并返回错误“得到错误空(子)表达式”。

这是我到目前为止所拥有的:

页面名称:search_results.php

<?PHP
//include db connection and functions file
require_once("lib/db.php");
require_once("lib/functions.php");

//Get POST vars from search form and drop down list

$name = "";
if(!empty($_POST['name'])){
    $name = mysql_real_escape_string(trim($_POST['name']));
}

if(!empty($_POST['address'])){
    $address = mysql_real_escape_string(trim($_POST['address']));
}

$type = "";
if(!empty($_POST['type'])){
    $type = mysql_real_escape_string(trim($_POST['type']));
}

//search form here. It redirects to this page, same as the drop down list which uses javascript to post vars to this same page

//This is the SQL query that is giving me the nightmares

$query="SELECT * FROM markers WHERE name REGEXP '^(".$name.")' OR address REGEXP '^(".$address.")' OR type REGEXP '^(".$type.")' AND active = 1";

$result=mysql_query($query) or die(mysql_error());
//display results here

问题是,如果数据库中没有与“类型”匹配,我会收到错误 Got error 'empty (sub)expression。任何援助将不胜感激

4

0 回答 0