搜索多个表 - 如果未填写输入,请将表保留

时间:2014-04-08 07:54:06

标签: php mysql

我有一个包含3个输入字段的页面,用于搜索数据库。但并非所有用户都会填写所有字段,因此我需要一种方法来确保数据库检查正常。

现在,我编写了8个不同的sql语句,并使用if语句检查填写了哪些字段。这样做有效,但我觉得必须有更好的做法。

搜索表单中的ID可以在我的数据库的其他表中找到,并加载了jQuery的自动完成功能。

现在使用的代码:

<form action="" method="post">

<div class="ui-widget">
    <section>
        <label for="tags">Trefwoord:</label>
        <input name="tags" id="tags"><input type="hidden" name="tags_id" class="tags_id" value="">
    </section>
    <section>
        <label for="categorie">Categorie:</label>
        <input name="cats" id="categorie"><input type="hidden" name="cats_id" class="cats_id" value="">
    </section>
    <section>
        <label for="competentie">Competentie:</label>
        <input name="com" id="competentie"><input type="hidden" name="com_id" class="com_id" value="">
    </section>

    <input type="submit" name="submit">
</div>


</form>

<?php
if(isset($_POST['submit'])) {

$trefwoord_id = $_POST['tags_id'];
$categorie_id = $_POST['cats_id'];

$p_trefwoord = $_POST['tags'];
$p_categorie = $_POST['cats'];

$competentie_id = $_POST['com_id'];
$p_comptentie = $_POST['com'];

if($trefwoord_id == null) {$sql = "SELECT * FROM spel_cat LEFT JOIN spel_com  ON spel_cat.spelid = spel_com.spelid WHERE '$categorie_id' = catid && '$competentie_id' = comid";}
if($categorie_id == null) {$sql = "SELECT * FROM spel_tw LEFT JOIN spel_com  ON spel_tw.spelid = spel_com.spelid WHERE '$trefwoord_id' = twid && '$competentie_id' = comid";}
if($competentie_id == null) {$sql = "SELECT * FROM spel_tw LEFT JOIN spel_cat  ON spel_tw.spelid = spel_cat.spelid WHERE '$trefwoord_id' = twid && '$categorie_id' = catid";}

if($trefwoord_id == null && $categorie_id == null) {$sql = "SELECT * FROM spel_com  WHERE '$competentie_id' = comid";}
if($trefwoord_id == null && $competentie_id == null) {$sql = "SELECT * FROM spel_cat  WHERE '$categorie_id' = catid";}
if($categorie_id == null && $competentie_id == null) {$sql = "SELECT * FROM spel_tw  WHERE '$trefwoord_id' = twid";}
if($trefwoord_id == null && $competentie_id == null && $categorie_id == null) {$sql = ""; echo "<b>Gebruik minstens 1 zoekterm</b>";}

if($trefwoord_id != null && $categorie_id != null && $competentie_id != null) {$sql = "SELECT * FROM (spel_tw  LEFT JOIN spel_cat  ON spel_tw.spelid = spel_cat.spelid) LEFT JOIN spel_com ON spel_cat.spelid = spel_com.spelid WHERE '$trefwoord_id' = twid && '$categorie_id' = catid && '$competentie_id' = comid";
}

if($sql != null) {
    $games = mysqli_query($link,$sql) or die(mysql_error());
    $num = mysqli_num_rows($games);

// AND SO ON...

工作代码(感谢OrangeHippo)

<?php
if(isset($_POST['submit'])) {

$trefwoord_id = $_POST['tags_id'];
$categorie_id = $_POST['cats_id'];

$p_trefwoord = $_POST['tags'];
$p_categorie = $_POST['cats'];

$competentie_id = $_POST['com_id'];
$p_comptentie = $_POST['com'];


$from = array();
$where = " 1 = 1 ";



if($trefwoord_id != null) {
    $from["str"] = "spel_tw str";
    $where .= " AND twid = '$trefwoord_id' ";
}
if($categorie_id != null) {
    $from["sca"] = "spel_cat sca";
    if (isset($from["str"])) {
        $where .= " AND sca.spelid = str.spelid ";
    }
    $where .= " AND catid = '$categorie_id' ";
}
if($competentie_id != null) {
    $from["sco"] = "spel_com sco";
    if (isset($from["str"])) {
        $where .= " AND sco.spelid = str.spelid ";
    }else if (isset($from["sca"])) {
        $where .= " AND sco.spelid = sca.spelid ";
    }
    $where .= " AND comid = '$competentie_id' ";
}



$sql = "SELECT * FROM " . implode(",", $from) . " WHERE $where";

if($trefwoord_id == null && $competentie_id == null && $categorie_id == null) {$sql = ""; echo "<b>Gebruik minstens 1 zoekterm</b>";}

//echo $sql;
if($sql != null) {
    $games = mysqli_query($link,$sql) or die(mysql_error());
    $num = mysqli_num_rows($games);

//AND SO ON ...

1 个答案:

答案 0 :(得分:1)

理想情况下,您将根据传递的值构建查询:

$from = array();
$where = " 1 = 1 ";

if($trefwoord_id != null) {
    $from["str"] = "spel_tw str";
    $where .= " AND twid = '$trefwoord_id' ";
}
if($categorie_id != null) {
    $from["sca"] = "spel_cat sca";
    if (isset($from["str"])) {
        $where = " AND sca.spelid = str.spelid ";
    }
    $where .= " AND catid = '$categorie_id' ";
}
if($competentie_id != null) {
    $from["sco"] = "spel_com sco";
    if (isset($from["str"])) {
        $where = " AND sco.spelid = str.spelid ";
    }else if (isset($from["sca"])) {
        $where = " AND sco.spelid = sca.spelid ";
    }
    $where .= " AND comid = '$competentie_id' ";
}



$query = "SELECT * FROM " . implode(",", $from) . " WHERE $where";

正如您所看到的那样,您的文本更少,使代码更清晰。如果您希望代码更加干净,可以使用一些查询构建器库,如doctrine2 DBAL