如何显示连接到数据库的表单搜索结果?

时间:2015-06-19 09:50:10

标签: php mysql phpmyadmin

我要向你呈现的代码不起作用。没有显示IF条件的结果。但是对于其他人而言。当我使用echo单独调用$ service_id时,它会为我提供我搜索过的ID但在WHERE close中使用时:service_id ='$ service_id'没有显示任何内容。

<form method="GET" name="pesquisa" id="inserrifo" action="/services.php">
    <input class="procurasalgo" id='pesqSign' name="keyword" value="<?php echo $keyword;?>" placeholder="Inserir palavra"/><label for="pesqSign" id="pesqSigne"><img class="lupapesquisa" src="/img/search-26.png" /></label>
    <select name="services">
        <option disabled selected value=''>Serviços</option>
        <?php
            $res = $DAL->mysqlQuery("SELECT * FROM services");
            while($row = mysql_fetch_assoc($res)){
                echo "<option value='".$row['serv_id']."'>".$row['serv_name']."</option>";
            }
        ?>
    </select>
    <select name='distritos'>
        <option disabled selected value=''>Portugal</option>
        <?php
            $res = $DAL->mysqlQuery("SELECT * FROM distritos");
            while($row = mysql_fetch_assoc($res)){
                echo "<option value='".$row['d_id']."'>".$row['d_nome']."</option>";
            }
        ?>
    </select>
    <button>Procurar</button>
</form>

    “placeholder =”Inserir palavra“/&gt;              SERVICOS         mysqlQuery(“SELECT * FROM services”);             while($ row = mysql_fetch_assoc($ res)){                 echo“”。$ row ['serv_name']。“”;             }         ?&GT;                   葡萄牙         mysqlQuery(“SELECT * FROM distritos”);             while($ row = mysql_fetch_assoc($ res)){                 echo“”。$ row ['d_nome']。“”;             }         ?&GT;          Procurar

<?php   

    $service_id = $_GET['services'];
    $district_id = $_GET['distritos'];
    $keywords = $_GET['keyword'];


    if ($service_id != '' || $district_id != '' || $keywords != '') {

    $result = $DAL->mysqlQuery("SELECT * FROM users, services, distritos, users_services WHERE u_id = user_id && service_id = '$service_id' && distrito_id = '$district_id' ");

    $row = mysql_fetch_assoc($result);

    while($row = mysql_fetch_assoc($result)) {

    $img_user = $row['u_foto'];


        echo "<div class='Services'><img src='http://mufip.pt/images/userimages/avatars/".$img_user."' /><br /><h5>".$row['u_name']."<h5><h6>".substr($row['u_descricao'],0, 140)."</h6></div>";

        } //while

    } //if

    else {

    $result = $DAL->mysqlQuery("SELECT * FROM users, services, distritos, users_services WHERE u_id = user_id && service_id = serv_id && distrito_id = d_id ORDER BY RAND()");

    $num = mysql_numrows($result);
    echo $num." resultados<br />";

    while($row = mysql_fetch_assoc($result)) {

    $img_user = $row['u_foto'];

    echo "<div class='Services'><img src='http://mufip.pt/images/userimages/avatars/".$img_user."' /><br /><h5>".$row['u_name']."<h5><h6>".substr($row['u_descricao'],0, 140)."</h6></div>";

        } //while

    } //else

    ?>

2 个答案:

答案 0 :(得分:0)

if ($service_id != '' || $district_id != '' || $keywords != '') {

行正在检查三者之间是否存在任何字段。换句话说,当$ service_id或$ district_id或$ keywords不为空时,此逻辑检查将为TRUE。 假设,用户仅为“关键字”输入值,在此页面中,您将只获得$ keywords和$ service_id的值,$ district_id将为空。

由于其中一个(此处为$ keywords)不为空,因此IF语句将返回true并且程序控制将在其中移动。

但是,在SQL查询中,您正在使用AND条件,并且在service_id&amp;中需要有效或非空值。 distrito_id列。由于其中一个可能保持为空,因此该查询不会产生任何结果。

要调试情况,请从下拉列表中选择一个值,然后使用echo语句在IF块内打印查询。

答案 1 :(得分:0)

我得到了朋友的帮助:)

public function returnSelectService($nomeSelect,$idSelect,$nomeCampo,$stringSQL,$selected = null)
{       
    $list = "<select class=\"selectServicesList\" id=\"$idSelect\" name=\"$nomeSelect\">";
    $list .="<option value=\"\">Serviços</option>";
    $res = $this->mysqlQuery($stringSQL);
    $select = "";


    while ($linha = mysql_fetch_object($res)) 
    {
        if($selected != null && $selected == $linha->serv_id)
            $select = 'selected="selected"';            
        $list .="<option $select  value=\"$linha->serv_id\">".$linha->$nomeCampo."</option>";
        $select = "";
    }
    $list .= "</select>";
    return  $list;
}



public function returnSelectDistrict($nomeSelect,$idSelect,$nomeCampo,$stringSQL,$selected = null)
{       
    $list = "<select class=\"selectServicesList\" id=\"$idSelect\" name=\"$nomeSelect\">";
    $list .="<option value=\"\">Portugal</option>";
    $res = $this->mysqlQuery($stringSQL);
    $select = "";


    while ($linha = mysql_fetch_object($res)) 
    {
        if($selected != null && $selected == $linha->d_id)
            $select = 'selected="selected"';            
        $list .="<option $select  value=\"$linha->d_nome\">".$linha->$nomeCampo."</option>";
        $select = "";
    }
    $list .= "</select>";
    return  $list;
}




    <form method="GET" name="pesquisa" id="inserrifo" action="/services.php">
            <br />
            <?php
            echo $DAL->returnSelectService('services','services','serv_name',"SELECT * FROM services ",$service_id);
            ?>          
            <?php
            echo $DAL->returnSelectDistrict('distritos','distritos','d_nome',"SELECT * FROM distritos ",$d_id); 
            ?>  
            <button class="btn btn-default">Procurar</button>
        </form>



    <?php   


$stringSQL ="SELECT * FROM users, services, distritos, users_services WHERE u_id = user_id AND service_id = serv_id AND distrito_id = d_id ";
if($service_id != "")
    $stringSQL .=" AND serv_id = '{$service_id}'  ";
if($district_id != "")
    $stringSQL .="AND d_id = '{$district_id}' ";
$stringSQL .="ORDER BY RAND()";


        $result = $DAL->mysqlQuery($stringSQL);

        $num = mysql_numrows($result);

            echo $num." resultados<br />";      

            while($row = mysql_fetch_assoc($result)) {

            $img_user = $row['u_foto'];

            echo "<div class='services'>
            <div class='servicesBox'>
            <img src='http://mufip.pt/images/userimages/avatars/".$img_user."' />
            <h4>".$row['u_name']."</h4><br />
            <h5>".$row['serv_name'].", ".$row['d_nome']."</h5><br />
            <p>".substr($row['u_descricao'],0, 200)." ...</p>
            </div>
            </div>
            <div style='clear: both; background-color: white; height: 15px'>
            </div>";

                }

            ?>