我有一個搜索功能腳本,用於搜索多個輸入字段。該腳本正在完美地搜索數據庫。問題是當我使用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