php - How to display results from form search, connected to database?

86

The code I'm going to present you is not working. No results are displaying for the IF condition. But for Else are. When I call $service_id alone with echo, it gives me the ID that I searched for but when used in the WHERE close: service_id = '$service_id' nothing is displayed.

<form method="GET" name="pesquisa" id="inserrifo" action="/services.php">
  <input id='pesqSign' name="keyword" value="<?php echo $keyword;?>" placeholder="Inserir palavra"/><label for="pesqSign" id="pesqSigne"><img 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"/> Serviços mysqlQuery("SELECT * FROM services"); while($row = mysql_fetch_assoc($res)){ echo "".$row['serv_name'].""; } ?> Portugal mysqlQuery("SELECT * FROM distritos"); while($row = mysql_fetch_assoc($res)){ echo "".$row['d_nome'].""; } ?> 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

    ?>
714

Answer

Solution:

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

line is checking presence of any fields between three. In other words this logical check will give you TRUE when $service_id or $district_id or $keywords is not empty. Suppose, user entered values for only "keywords" and in this page, you will get value of only $keywords and $service_id and $district_id will be empty.

Since one of them (here $keywords) is not empty, IF statement will return true and program control will move inside it.

However , in the SQL query you are using AND condition and that requires valid or non-empty value in service_id & distrito_id columns. Since one of them might remain empty, this query will produce no result.

To debug the situation, select value from one of the drop-down only and use echo statement to print the query inside IF block.

138

Answer

Solution:

I had help from a friend :)

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>";

                }

            ?>

People are also looking for solutions to the problem: php - MySQL, CONCAT, the result is null sometimes

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.