使用选项/选择HTML表单标签PHP MySQL多重搜索查询 [英] PHP MySQL multiple search query using option / select HTML form tags

查看:87
本文介绍了使用选项/选择HTML表单标签PHP MySQL多重搜索查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用两个搜索字段设置基本的MySQL LIKE搜索。我不想拥有它,因此它有多个可选的搜索字段,例如 if(isset($ _ POST ['city'])|| isset($ _ POST ['name']))我不确定如何用HTML < select name =>< option value => ... 因为您无法为每个< select> 字段。因此,首先...你如何制作一个MySQL < select> 搜索,其中每个产生它自己的结果?那么,我将如何正确查询多个搜索选项?在下面,我使用了文本框+ < select> search。

 < input type =textname =type/> 
< select name =location>
< option value =Chicago> Chicago< / option>
< option value =纽约>纽约< / option>
< / select>
< input type =submitvalue =Search/>

MySQL:

  $ query =选择
名称,类型,描述,位置,zip_code,phone_number
从搜索
WHERE类型LIKE'%$ type%'
OR位置像'芝加哥'
或位置'纽约'
;

我知道我在OR子句上做错了什么。我不完全确定如何正确执行< option> /< select> HTML表单标记搜索查询。

解决方案

HTML

 < select multiple name =location []大小=2> 

PHP

< pre = $ w = array();
$ where ='';
foreach($ _POST ['location'] as $ loc){
$ loc $ mysql $ _ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ {
$ w [] =type LIKE'%。mysql_real_escape_string($ _ POST ['type'])。%';
}
if($ w)$ where = implode('OR',$ w);


I'm trying to set up a basic MySQL LIKE search using two search fields. I wan't to have it so it's multiple optional search fields e.g. if(isset($_POST['city']) || isset($_POST['name'])) I'm not sure how to do it with a HTML<select name=""><option value="">... seeing as you can't define a name for each <select> field. So, first... How do you make a MySQL <select>search where each yields it's own result? Then, how would I properly query multiple search options? Below I'm using a "text box" + <select> search.

            <input type="text" name="type" />  
            <select name="location">
               <option value="Chicago">Chicago</option>
               <option value="New York">New York</option>
            </select>
            <input type="submit" value="Search" />

MySQL:

              $query = "SELECT 
              name, type, description, location, zip_code, phone_number
              FROM search
              WHERE type LIKE '%$type%'
              OR location LIKE 'Chicago'
              OR location LIKE 'New York'
              ";

I know I'm doing something wrong on the OR clauses. I"m not totally sure how to properly do the <option> / <select> HTML form tag search query.

解决方案

HTML

<select multiple name="location[]" size="2">

PHP

$w     = array();
$where = '';
foreach ($_POST['location'] as $loc){
  $loc = mysql_real_escape_string($loc);
  $w[] = "location = '$loc'";
}
if (!empty($_POST['type'])){
  $w[] = "type LIKE '%".mysql_real_escape_string($_POST['type'])."%'";
}
if ($w) $where = implode(' OR ',$w);

这篇关于使用选项/选择HTML表单标签PHP MySQL多重搜索查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆