使用单选按钮过滤搜索PHP PHP SQL [英] filter a search with radio button PHP SQL
问题描述
我有一个有效的搜索列表属性.该搜索根据找到的单词是否与姓名,家庭和工作相似来进行.现在我想分开搜索.我的意思是,创建三个单选按钮.点击第一个按钮按名称搜索,第二个按钮按家庭搜索,第三个按钮按工作搜索时.
i have a search list that works property. theh search works according to finding if searched word is like name, family and job. now i want too seprate this searching. i mean, creat three radio button. when cleck on first button search by name, second one, search by family, and third one search by job.
所以我的基本代码在这里:
so my base code is here:
这里需要添加三个单选按钮. (searchform.php)
here need to add three radio button. (searchform.php)
<form name="form1" dir="rtl" method="post" action="searchresults.php">
<label for="search"> search </label>
<input name="search" type="text" size="40" maxlength="50" placeholder="you can search">
<input type="submit" name="submit" value="search"/> <br/>
</form>
搜索鳕鱼在这里:
searchresult.php
searchresult.php
<?php
$db_host = 'localhost';
$db_name= 'site';
$db_table= 'tablesite';
$db_user = 'root';
$db_pass = '';
$con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");
$selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
mysql_query("SET CHARACTER SET utf8");
$dbresult=mysql_query("SELECT tablesite.name,
tablesite.family,
job_list.job_name,
FROM $db_table
INNER JOIN relation
on tablesite.id_user=relation.user_id
INNER JOIN job_list
on relation.job_id=job_list.job_id
WHERE job_name LIKE '%".$_POST['search']."%' OR
name LIKE '%".$_POST['search']."%' OR
family LIKE '%".$_POST['search']."%'",$con);
?>
<title>نتایج جستجو</title><fieldset class="fdex" dir="rtl">
<legend><span class="style4">نتایج جستجوی مشاغل</span></legend>
<?php
while ($row = mysql_fetch_array($dbresult, MYSQL_ASSOC)) {
printf("نام: %s %s     شغل: %s     شماره تلفن: %s <br>", $row["name"], $row["family"], $row["job_name"], $row["phone_number"]);
}
?>
如您所见,
在这种搜索方法中,当用户在搜索框中输入示例学校时,它将按学校查找可能是一个人的家庭的所有结果.所以,我需要将此搜索分为三个广播按钮.名字,第二家庭,第三份工作.
as you see, in this search method, when user write in search box, example school, it find all results by school that may be family of a person. soo i need to seprate this search to three radio buttion. first name, second family, third job.
推荐答案
只需将单选按钮添加到表单中即可.
Just add the radio buttons to the form.
<form name="form1" dir="rtl" method="post" action="searchresults.php">
<label for="search"> search </label>
<input name="search" type="text" size="40" maxlength="50" placeholder="you can search">
<input type="radio" name="search_type" value="job_name" checked="checked">Job<br>
<input type="radio" name="search_type" value="family">Family<br>
<input type="radio" name="search_type" value="name">Name
<input type="submit" name="submit" value="search"/> <br/>
</form>
然后在您的sql查询中使用post字段和值. mysql已贬值,因此您应该使用mysqli,并且必须转义输入以防止注入攻击.
And use the post field and value in your sql query. mysql is depreciated so you should use mysqli and it is essential that you escape inputs to prevent injection attacks.
$field = mysql_real_escape_string($_POST['search_type']);
$value = mysql_real_escape_string($_POST['search']);
$sql = "SELECT tablesite.name,
tablesite.family,
job_list.job_name,
FROM $db_table
INNER JOIN relation
on tablesite.id_user=relation.user_id
INNER JOIN job_list
on relation.job_id=job_list.job_id
WHERE
$field LIKE '%".$value."%'";
$dbresult=mysql_query($sql,$con);
您可能还想在运行查询之前验证搜索值,并在遍历每一行之前检查结果.
You may also like to validate a search value before running the query and check for a result before looping through each row.
这篇关于使用单选按钮过滤搜索PHP PHP SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!