0

我有一个使用 OR 构建的搜索查询,但我想使用 AND 来获得更少的结果。不幸的是,由于某些参数(VatStatut、VarDomaine 等)在输入表单中为空,我根本没有得到任何结果。如何过滤掉这些空参数?

$VarStatut_jeu_res1 = "%";
if (isset($_POST['choix_statut'])) {
$VarStatut_jeu_res1 = $_POST['choix_statut'];
}

$VarSujet_jeu_res1 = "%";
if (isset($_POST['choix_sujet'])) {
$VarSujet_jeu_res1 = $_POST['choix_sujet'];
}

$VarDateEcheance1_jeu_res1 = "%";
if (isset($_POST['choix_dateecheance1'])) {
$VarDateEcheance1_jeu_res1 = $_POST['choix_dateecheance1'];
}

$VarDateEcheance2_jeu_res1 = "%";
if (isset($_POST['choix_dateecheance2'])) {
$VarDateEcheance2_jeu_res1 = $_POST['choix_dateecheance2'];
}

$VarDateOrigine1_jeu_res1 = "%";
if (isset($_POST['choix_dateorigine1'])) {
$VarDateOrigine1_jeu_res1 = $_POST['choix_dateorigine1'];
}

$VarDateOrigine2_jeu_res1 = "%";
if (isset($_POST['choix_dateorigine2'])) {
$VarDateOrigine2_jeu_res1 = $_POST['choix_dateorigine2'];
}

$VarTheme_jeu_res1 = "%";
if (isset($_POST['choix_theme'])) {
$VarTheme_jeu_res1 = $_POST['choix_theme'];
}

$VarAuteur_jeu_res1 = "%";
if (isset($_POST['choix_auteur'])) {
$VarAuteur_jeu_res1 = $_POST['choix_auteur'];
}

$VarDomaine_jeu_res1 = "%";
if (isset($_POST['choix_domaine'])) {
$VarDomaine_jeu_res1 = $_POST['choix_domaine'];
}

mysql_select_db($database_ConnJournal, $ConnJournal);

$query_jeu_res1 = sprintf("SELECT * FROM Projets1 WHERE Projets1.Statut LIKE %s OR       Projets1.Domaine LIKE %s OR Projets1.Auteur LIKE %s OR Projets1.Theme LIKE %s OR Projets1.DateOrigine BETWEEN %s AND %s OR Projets1.DateEcheance BETWEEN %s AND %s OR MATCH (Sujet) AGAINST (%s) ORDER BY Projets1.DateHeure DESC", 
4

1 回答 1

0

尝试使用empty()而不是isset. 这样,如果变量存在但为空字符串,则不会设置参数。

IE

$VarDomaine_jeu_res1 = "%";
if (!empty($_POST['choix_domaine'])) {
    $VarDomaine_jeu_res1 = $_POST['choix_domaine'];
}

(注意我们使用的是!empty()

于 2013-01-31T21:42:42.360 回答