php - How to display results from form search, connected to database?
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
?>
Answer
Solution:
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.
Answer
Solution:
I had help from a friend :)