我有一个搜索多个输入字段的搜索功能脚本。该脚本正在完美地搜索数据库。问题是当我使用OR运算符时我必须输入要搜索的两个字段并且结果显示正常但如果我只输入1个字段则搜索不起作用并显示记录。如果我使用AND运算符,我必须输入1个字段并显示相应的结果,但是当我输入2个字段时,结果为NULL。 AND& OR运营商给我横向结果。
我希望当我输入1个字段时,应显示匹配结果,当我输入两个字段时,应显示与两个字段匹配的结果。
这是我的index.php搜索脚本。
//Perform Search from our database
if(isset($_POST['action_type']))
{
echo '';
if ($_POST['action_type'] == 'search')
{
$search = mysqli_real_escape_string($link, strip_tags($_POST['searchText']));
$search_add = mysqli_real_escape_string($link, strip_tags($_POST['searchAddress']));
$sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name,
contact_no, residential_address,
company, company_address from tblcontact
where CONCAT(first_name, ' ', Last_name) like '%$search%' or contact_no like '%$search_add%' order by contact_id asc";
$result = mysqli_query($link, $sql);
if(!$result)
{
echo mysqli_error($link);
exit();
}
//Loop through each row on array and store the data to $contact_list[]
while($rows = mysqli_fetch_array($result))
{
$contact_list[] = array('contact_id' => $rows['contact_id'],
'contact_name' => $rows['contact_name'],
'contact_no' => $rows['contact_no'],
'residential_address' => $rows['residential_address'],
'company' => $rows['company'],
'company_address' => $rows['company_address']);
}
include 'contactlist.php';
exit();
}
}
这是我的contactlist.php,其格式为。
<center><div style="margin-bottom: 5px;">
<form method="POST" action="index.php" >
<table>
<tr>
<th>Name</th>
<td><input type="text" id="searchText" name="searchText" style="width: 300px; margin-left: 14px;"/></td>
</tr>
<tr>
<th>Contact</th>
<td><input type="text" id="searchAddress" name="searchAddress" style="width: 300px; margin-left: 14px;"/></td>
</tr>
<input type="hidden" name="action_type" value="search"/>
</table><br>
<input type="submit" value="Refresh Search" onClick="window.location.href='index.php'">
</form>
</div>
<div style="max-height: 350px; overflow:auto;">
<table class="pbtable" border="1">
<thead>
<tr>
<th>
ID
</th>
<th>
Name
</th>
<th>
Contact #
</th>
<th>
Res. Address
</th>
<th>
Company
</th>
<th>
Company Address
</th>
<th></th><th></th>
</tr>
</thead><br><br><br>
<tbody>
<?php foreach($contact_list as $contact) : ?>
<tr>
<td>
<?php echo $contact["contact_id"]; ?>
</td>
<td>
<?php echo $contact["contact_name"]; ?>
</td>
<td>
<?php echo $contact["contact_no"]; ?>
</td>
<td>
<?php echo $contact["residential_address"]; ?>
</td>
<td>
<?php echo $contact["company"]; ?>
</td>
<td>
<?php echo $contact["company_address"]; ?>
</td>
<td>
<form method="post" action="index.php">
<input type="hidden" name="ci"
value="<?php echo $contact["contact_id"]; ?>" />
<input type="hidden" name="action" value="edit" />
<input type="submit" value="Edit" />
</form>
</td>
<td>
<form method="POST" action="index.php"
onSubmit="return ConfirmDelete();">
<input type="hidden" name="ci"
value="<?php echo $contact["contact_id"]; ?>" />
<input type="hidden" name="action" value="delete" />
<input type="submit" value="Delete" />
</form>
</td>
<tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
如果输入了1个字段,并且输入了2个字段,则搜索应该起作用。这个问题的解决方案是什么?
答案 0 :(得分:0)
请尝试以下版本的SQL查询:
$sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name,
contact_no, residential_address,
company, company_address from tblcontact
where CONCAT(first_name, ' ', Last_name) like '%"
.(empty($search) ? "" : ($search."%"))
."' AND contact_no like '%"
.(empty($search_add) ? "" : ($search_add."%"))
."' order by contact_id asc";
答案 1 :(得分:0)
您需要检查字段是否为空:
if(!empty($_POST['searchText'])
$search = mysqli_real_escape_string($link, strip_tags($_POST['searchText']));
if(!empty($_POST['searchAddress'])
$search_add = mysqli_real_escape_string($link, strip_tags($_POST['searchAddress']));
然后,根据这个,您应该更改查询。
if(!empty($_POST['searchText'] && !empty($_POST['searchAddress'])
{
$sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name,
contact_no, residential_address,
company, company_address from tblcontact
where CONCAT(first_name, ' ', Last_name) like '%$search%' or contact_no like '%$search_add%' order by contact_id asc";
}
else if(empty($_POST['searchText'] && !empty($_POST['searchAddress'])
{
$sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name,
contact_no, residential_address,
company, company_address from tblcontact
where contact_no like '%$search_add%' order by contact_id asc";
}
else if(!empty($_POST['searchText'] && empty($_POST['searchAddress'])
{
$sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name,
contact_no, residential_address,
company, company_address from tblcontact
where CONCAT(first_name, ' ', Last_name) like '%$search%' order by contact_id asc";
}
根据您为AND案例尝试实现的目标调整查询。