如何将搜索框和下拉列表组合到输出结果(php,mysql)
I have created an HTML form with a search box and a drop down list of 50 states in the USA. What I want is to let the user type in a keyword in the search box (email, subject, username, etc.) and select a state from the drop down. By selecting a state from the drop down, I want my PHP code to find the keyword from the search box only in that selected state. For example as a user, I would like to find "soccer" in the state of Washington and see a list of results.
In other words, how would I display results only in the selected state combined with my search form?
Here is a bit of my PHP code to give you all an idea of what I'm trying to accomplish.
// this is pulling the text that was typed in the html search box
$searchbox = $_POST['searchbox'];
// this is pulling the state from my HTML drop down menu
$state = $_POST['state'];
$searchboxresult = mysql_query("
SELECT *
FROM table
WHERE (ID LIKE '%".$_POST['searchbox']."%')
OR (stateID LIKE '%".$_POST['searchbox']."%')
OR (city LIKE '%".$_POST['searchbox']."%')
OR (subject LIKE '%".$_POST['searchbox']."%')
OR (email LIKE '%".$_POST['searchbox']."%')
OR (posting LIKE '%".$_POST['searchbox']."%')
OR (skill LIKE '%".$_POST['searchbox']."%')
OR (event LIKE '%".$_POST['searchbox']."%')
OR (days LIKE '%".$_POST['searchbox']."%')
OR (url LIKE '%".$_POST['searchbox']."%')
");
// and
$statedropdownresult = mysql_query("
SELECT * FROM table WHERE stateID LIKE '%$state%'
");
Then I have a while loop that searches for everything and outputs the data. But I can't get the states drop down to limit what the user is searching for. I can only get one or the other mysql_query to work in combination with my while loop, not both. For some reason, combining the queries into one using AND doesn't seem to work either. Any ideas?
I know that the code is sloppy and there are security issues, however I will be going through to do a cleanup later on. If you need more details or clarification on something, I would be happy elaborate more!
我创建了一个HTML表单,其中包含一个搜索框和一个美国50个州的下拉列表。 我想要的是让用户在搜索框中键入关键字(电子邮件,主题,用户名等),然后从下拉列表中选择一个州。 通过从下拉列表中选择状态,我希望我的PHP代码仅在该选定状态下从搜索框中查找关键字。 例如,作为用户,我想在华盛顿州找到“足球”,并查看结果列表。 p>
换句话说,我如何仅在选择状态下结合我的搜索表单显示结果? p>
这是我的一些PHP代码 让你对我想要完成的事情有所了解。 p>
//这是拉动在html搜索框中键入的文本
$ searchbox = $ _POST ['searchbox'];
//这是 从我的HTML下拉菜单中拉出状态
$ state = $ _POST ['state'];
$ searchboxresult = mysql_query(“
SELECT *
FROM FROM table
WHERE(ID LIKE'%”。 $ _POST ['searchbox']。“%')
OR(stateID LIKE'%”。$ _ POST ['searchbox']。“%')
OR(city LIKE'%”。$ _ POST ['searchbox' ]。“%')
OR(主题LIKE'%”。$ _ POST ['searchbox']。“%')
OR(电子邮件LIKE'%”。$ _ POST ['searchbox']。“%')
OR(发布LIKE'%'。$ _ POST ['searchbox']。“%')
OR(技能LIKE'%”。$ _ POST ['searchbox']。“%')
OR(事件LIKE '%'。$ _ POST ['searchbox']。“%')
OR(天喜欢'%'。$ _ POST ['searchbox']。”%')
OR(url LIKE'%“。$ _ POST ['searchbox']。“%')
”);
//和
$ statedropdownresult = mysql_query(“
SELECT * FROM table WHERE stateID LIKE'%$ state%'
” );
code> pre>
然后我有一个while循环搜索所有内容并输出数据。 但我不能让状态下降来限制用户搜索的内容。 我只能将一个或另一个mysql_query与我的while循环结合使用,而不是两者兼而有之。 出于某种原因,使用 AND b>将查询合并为一个似乎也不起作用。 有什么想法? p>
我知道代码很草率并且存在安全问题,但是我将在稍后进行清理。 如果您需要更多细节或澄清某些事情,我会很高兴详细说明! p>
div>
You could try something like this:-
$sql ="SELECT * FROM table WHERE 1";
if($_POST['searchbox'] != ''){
$sql =" and (ID LIKE '%".$_POST['searchbox']."%') OR (stateID LIKE
'%".$_POST['searchbox']."%') OR (city LIKE '%".$_POST['searchbox']."%') OR
(subject LIKE '%".$_POST['searchbox']."%') OR (email LIKE
'%".$_POST['searchbox']."%') OR (posting LIKE '%".$_POST['searchbox']."%') OR
(skill LIKE '%".$_POST['searchbox']."%') OR (event LIKE
'%".$_POST['searchbox']."%') OR (days LIKE '%".$_POST['searchbox']."%') OR (url
LIKE '%".$_POST['searchbox']."%')";
}
if($state != ''){
$sql .= " and stateID LIKE '%".$state."%'";
}
mysql_query($sql);
Hope this helps.