我有一个搜索功能脚本,用于搜索多个输入字段。该脚本正在完美地搜索数据库。问题是当我使用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个字段,则搜索应起作用。这个问题的解决方案是什么?
即使我最近遇到同样的问题..我用'if'条件来克服它..我建议你也使用条件.. 'if($ variable!=“”){//执行一个查询} elseif($ variable2!==“”){//执行另一个查询}' –
使用我的脚本显示它请 – Kanishk