从我的编码中,我可以按一个关键字进行搜索。我可以按护照或捐赠日期进行搜索。但是,如果我想同时搜索两者,那我应该修改什么。预先感谢。
代码
if (isset($_GET['q'])) {
$q = $_GET['q'];
$statement = $db->prepare("select * from donate where passport_IC like :passport_IC or donation_date like :donation_date ORDER BY `donation_date` DESC
");
$statement->execute([
':passport_IC' => '%' . $q .'%',
':donation_date' => '%' . $q .'%'
]);
} else {
$statement = $db->prepare('SELECT * from donate');
$statement->execute();
}
$people = $statement->fetchAll(PDO::FETCH_OBJ);
?>
<?php foreach($people as $person): ?>
<tr>
<td><?php echo $person->passport_IC; ?></td>
<td><?php echo $person->donation_date; ?></td>
</tr>
<?php endforeach; ?>
答案 0 :(得分:0)
根据我的想法,您可以尝试
$passport_IC = '';
$donation_date = '';
$condition = " ORDER BY `donation_date` DESC";
if(!empty($passport_IC) && !empty($donation_date)){
$condition = " WHERE passport_IC LIKE '%$passport_IC%' OR
donation_date LIKE '%$donation_date%' ORDER BY `donation_date` DESC";
}elseif(empty($passport_IC) && !empty($donation_date)){
$condition = " WHERE passport_IC LIKE '%$donation_date%' OR
donation_date LIKE '%$donation_date%' ORDER BY `donation_date` DESC";
}else{
$condition = " WHERE passport_IC LIKE '%$passport_IC%' OR
donation_date LIKE '%$passport_IC%' ORDER BY `donation_date` DESC";
}
$sql = "SELECT * FROM donate ".$condition;
OR
if(!empty($passport_IC) && !empty($donation_date)){
$condition = " WHERE passport_IC LIKE '%$passport_IC%') OR (donation_date LIKE '%$donation_date%' ORDER BY `donation_date` DESC";
}elseif(empty($passport_IC) && !empty($donation_date)){
$condition = " WHERE donation_date LIKE '%$donation_date%' ORDER BY `donation_date` DESC";
}else{
$condition = " WHERE donation_date LIKE '%$passport_IC%' ORDER BY `donation_date` DESC";
}
$sql = "SELECT * FROM donate ".$condition;
设置其格式以支持准备好的语句。