PHP、sql动态查询和可能的LIKE and OR一起无法正常工作
我有一个搜索表(搜索财产、房地产): HTML:标签已放置PHP、sql动态查询和可能的LIKE and OR一起无法正常工作,php,mysql,search,operators,combinations,Php,Mysql,Search,Operators,Combinations,我有一个搜索表(搜索财产、房地产): HTML:标签已放置 图斯 特价 租 图斯 公寓 双工 演播室 别墅 图斯 古莱特酒店 迦玛特 拉马尔萨 埃尔穆鲁伊 现在search-results.php: // Saving search terms in variables $keyword = strtolower(htmlspecialchars($_POST['keywords'])); // string from text field $status =
图斯
特价
租
图斯
公寓
双工
演播室
别墅
图斯
古莱特酒店
迦玛特
拉马尔萨
埃尔穆鲁伊
现在search-results.php:
// Saving search terms in variables
$keyword = strtolower(htmlspecialchars($_POST['keywords'])); // string from text field
$status = htmlspecialchars($_POST['status']); // 0 or 1
$type = htmlspecialchars($_POST['type']); // string value from option
$emplacement = htmlspecialchars($_POST['emplacement']); // string value from option
$surface_min = htmlspecialchars($_POST['surface_min']); // integer from text field
$surface_max = htmlspecialchars($_POST['surface_max']);// integer from text field
$chambres_min = htmlspecialchars($_POST['rooms_min']);// integer from text field
$chambres_max = htmlspecialchars($_POST['rooms_max']);// integer from text field
$prix_min = htmlspecialchars($_POST['price_min']);// integer from text field
$prix_max = htmlspecialchars($_POST['price_max']);// integer from text field
// create an empty array
$where_array = array();
// if user type in or choose an option in the search form
// > create a string with its value and put it in $where_array
if($keyword != '') $where_array[] = ' LOWER(prop_title) LIKE "%'.$keyword.'%" OR LOWER(prop_desc) LIKE "%'.$keyword.'%"';
if($status != '') $where_array[] = 'prop_status = '.$status.'';
if($type != '') $where_array[] = 'prop_type = "'.$type.'"';
if($emplacement != '') $where_array[] = 'prop_place = "'.$emplacement.'"';
if($surface_min != '') $where_array[] = 'prop_surface >= '.$surface_min.'';
if($surface_max != '') $where_array[] = 'prop_surface <= '.$surface_max.'';
if($rooms_min != '') $where_array[] = 'prop_rooms >= '.$rooms_min.'';
if($rooms_max != '') $where_array[] = 'prop_rooms <= '.$rooms_max.'';
if($price_min != '') $where_array[] = 'prop_price >= '.$price_min.'';
if($price_max != '') $where_array[] = 'prop_price <= '.$price_max.'';
// join all the array elements (the strings) saved in $where_array by the string : AND
$where_string = implode(" AND ", $where_array);
// the sql query
$search = "SELECT * FROM proprietes WHERE $where_string";
$search = $db->query($search);
// if the query exists
if($search)
{
// loop through all the properies that matches the WHERE conditions
foreach($search as $search_result)
{
// echo the title of the property
echo htmlspecialchars($search_result['prop_title']);
}
}
else
{
// which one is right ?
echo 'Error occured !';
echo 'No property found !';
}
//在变量中保存搜索项
$keyword=strtolower(htmlspecialchars($_POST['keywords'));//文本字段中的字符串
$status=htmlspecialchars($_POST['status']);//0或1
$type=htmlspecialchars($_POST['type']);//选项中的字符串值
$emposition=htmlspecialchars($_POST['emposition']);//选项中的字符串值
$surface\u min=htmlspecialchars($\u POST['surface\u min']);//文本字段中的整数
$surface\u max=htmlspecialchars($\u POST['surface\u max']);//文本字段中的整数
$chambres_min=htmlspecialchars($_POST['rooms_min']);//文本字段中的整数
$chambres_max=htmlspecialchars($_POST['rooms_max']);//文本字段中的整数
$prix_min=htmlspecialchars($_POST['price_min']);//文本字段中的整数
$prix_max=htmlspecialchars($_POST['price_max']);//文本字段中的整数
//创建一个空数组
$where_array=array();
//如果用户在搜索表单中键入或选择一个选项
//>创建一个字符串及其值并将其放入$where\u数组中
如果($keyword!='')$where_数组[]='较低(属性标题)如“%”.$keyword.'%”或较低(属性描述)如“%”.$keyword.'%'”;
如果($status!='')$where_数组[]='prop_status='.$status';
如果($type!='')$where_数组[]='prop_type=“”.$type.”;
如果($emposition!='')$where_数组[]='prop_place=“”.$emposition.”;
如果($surface_-min!='')$where_-array[]='prop_-surface>='.$surface_-min';
如果($surface_max!='')$where_array[]='prop_surface='.$rooms_min';
如果($rooms_max!='')$where_array[]='prop_rooms='。$price_min';
如果($price_max!='')$where_array[]='prop_price debugDumpParams()代码>以响应此操作:
我想是我的逻辑和语法都错了!需要帮忙吗
谢谢。这里有很多,但是您的输出查询显示了问题所在
未指定的布尔标准(和
和或
)使用不正确。应该是这样的
SELECT * FROM proprietes
WHERE prop_status = 1
AND (LOWER(prop_title) LIKE "%villa%" OR LOWER(prop_desc) LIKE "%villa%")
您需要更改数组中的逻辑,使其行为更像这样。换句话说,如果查询中有或
,则应将其括在括号内我认为如果将这行代码更改为
if($keyword != '')
$where_array[] = ' (LOWER(prop_title) LIKE "%'.$keyword.'%" OR LOWER(prop_desc) LIKE "%'.$keyword.'%")';
它将更正您的查询
如果使用双引号,则不需要所有的
串联,代码也会更易于阅读
if($keyword != '')
$where_array[] = " (LOWER(prop_title) LIKE '%$keyword%' OR LOWER(prop_desc) LIKE '%$keyword%')";
尝试将关键字搜索放在括号中,如(较低的(prop_title)如“%villa%”或较低的(prop_desc)如“%villa%”和prop_status=1
将回显查询放入phpmyadmin(或任何您使用的)并运行查询,在那里调试它,然后重构查询,然后重构相应的代码。为什么不干脆创建一个字符串,而不是一个$where\u数组,然后再进行内爆呢?只是说说而已<代码>和
优先于或
。使用括号来解决问题。@mim谢谢,就这样@RiggsFolly我对这个很陌生,你的评论听起来像中文,哈哈,我在wamp中使用phpmyadmin,我想下次我会用谷歌搜索“用phpmyadmin调试sql查询”^^^就是这样,它成功了!我应该记得我的数学;)至于LOWER,我仍然使用wamp在本地工作,我确实需要它,我没有先测试它,发现LIKE是区分大小写的!这就是为什么。但我不是很确定,因为我对这个很陌生!谢谢^^^至于较低的版本,它可能取决于排序,所以我将暂时保持原样
if($keyword != '')
$where_array[] = " (LOWER(prop_title) LIKE '%$keyword%' OR LOWER(prop_desc) LIKE '%$keyword%')";