来自HTML表单的PHP搜索代码
几分钟前,我在这里有一个关于排序的语法错误的问题。我需要帮助让这个脚本工作,或者至少有人给我指出正确的方向 这是一个按多个字段搜索的搜索脚本。search()数组工作正常,是一系列带有以下代码的复选框:来自HTML表单的PHP搜索代码,php,mysql,Php,Mysql,几分钟前,我在这里有一个关于排序的语法错误的问题。我需要帮助让这个脚本工作,或者至少有人给我指出正确的方向 这是一个按多个字段搜索的搜索脚本。search()数组工作正常,是一系列带有以下代码的复选框: <td width="22"><input type="checkbox" name="search[olevel = 'Yes']" id="search[olevel = 'Yes']" value="1"/> <input name="postcode[]"
<td width="22"><input type="checkbox" name="search[olevel = 'Yes']" id="search[olevel = 'Yes']" value="1"/>
<input name="postcode[]" type="text" id="postcode[]" size="12" maxlength="12" /></td>
“邮政编码”框是包含以下代码的文本框:
<td width="22"><input type="checkbox" name="search[olevel = 'Yes']" id="search[olevel = 'Yes']" value="1"/>
<input name="postcode[]" type="text" id="postcode[]" size="12" maxlength="12" /></td>
当我勾选olevel框时,它返回olevel字段中有Yes的所有记录。这正是我所期望的
如果我在邮政编码框中输入任何内容,它将不会返回任何结果
下面是搜索引擎的php代码
<?php
include ('c1.php');
if ($_COOKIE["auth"] == "1") {
$display_block = "<p>You are an authorized user.</p>";
} else {
header("Location: userlogin.html");
exit;
}
doDB();
$display_block = "<h1>Results</h1>";
if (isset($_POST['search']) && !empty($_POST['search'])) {
foreach ($_POST['search'] as $key => $value) {
if ($value == 1)
$search[] = "$key";
$searchstring = implode(' AND ', $search);
$post_map = array(
'postcode' => 'candididate_contact_details.postcode'
);
}
if (isset($_POST['postcode']) && !empty($_POST['postcode'])) {
foreach ($_POST['postcode'] as $key => $value) {
if (array_key_exists($key, $post_map))
$search[] = $post_map[$key] . '=' . mysql_real_escape_string($value);
echo $searchstring;
echo $search;
$query = "SELECT candidate_id.master_id, candidate_contact_details.first_name, candidate_contact_details.last_name, candidate_contact_details.home_phone, candidate_contact_details.work_phone, candidate_contact_details.mobile_phone, candidate_contact_details.email FROM candidate_id, candidate_contact_details, qualifications, security_experience, previous_career WHERE qualifications.active = 'finished' and candidate_id.master_id = candidate_contact_details.master_id and candidate_id.master_id = qualifications.master_id and candidate_id.master_id = security_experience.master_id and candidate_id.master_id = previous_career.master_id and $searchstring";
$query_res = mysqli_query($mysqli, $query)
or die(mysqli_error($mysqli));
// $search = mysqli_query($mysqli, $query)or die(mysqli_error($mysqli));
{
$display_block .= "
<table width=\"98%\" cellspacing=\"2\" border=\"1\">
<tr>
<th>Registration Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Home Number</th>
<th>Work Number</th>
<th>Mobile Number</th>
<th>E-Mail</th>
</tr>";
while ($result = mysqli_fetch_array($query_res)) {
$regnum = $result['master_id'];
$first_name = $result['first_name'];
$last_name = $result['last_name'];
$home_phone = $result['home_phone'];
$work_phone = $result['work_phone'];
$mobile_phone = $result['mobile_phone'];
$email = $result['email'];
$display_block .= "
<tr>
<td align=\"center\">$regnum <br></td>
<td align=\"center\">$first_name <br></td>
<td align=\"center\">$last_name <br></td>
<td align=\"center\">$home_phone <br></td>
<td align=\"center\">$work_phone <br></td>
<td align=\"center\">$mobile_phone <br></td>
<td align=\"center\">$email <br></td>
</tr>";
}
$display_block .= "</table>";
}
}
}
}
?>
<html>
<head>
<title> Display results</title>
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>
显示结果
我知道我做错了什么,但我不能完全理解。提前谢谢。如果我没有弄错的话,您每个表单都有几个邮政编码 那么
id=“postcode[]”
就错了,因为dom中会有几个id被命名为相同的id
只需从代码中删除即可。
name=“search[olevel='Yes']”“
我认为这不是一个有效的名称,但必须有人支持我。这是目前有效的部分。试图弄清楚这一点,因为我找到的所有搜索示例脚本都处理数据库中的1个字段或所有字段。但是为什么要使用search[olevel='Yes']
?不仅仅是搜索[1/0]
也可以吗?我之所以这么问,是因为当解析器无法正确理解脚本时,这些东西有时会在幕后破坏脚本。不。只有一个邮政编码。postcode是一个表示区域的文本字段,即HA2。需要能够使用olevel搜索该邮政编码区域中的每个人。