I have a page where people in a site selects search items.its a dating site.
so there is this first drop down menu for gender which u select three options:thus looking for
(all,Men,female) and another drop menu you select country which has two options:thus Country(all,.name of all the countries).
Men i denoted by symbol P and Female K and they are all served in a column Gender.
also countries are served in a column country in mysql database in table known as login.
I tried the code below for searching as illustrated above but it does not work.
Please i need your Help,Where i`m i getting wrong or which is the best way of doing this??

<?php
 
	include"config.php";
	

$gender=$_POST['gender'];
$sercountry=$_POST['country'];
trim($gender);
trim($sercountry);








if(!$sercountry||!$gender){
echo "<font color=brown><b>You have not entered ALL search details.<b></font></br>
<font color=Brown><b>Please go back and
      try again</b></font>";
	 exit;
	 }
	 
	
 if($gender==all||$sercountry==all){
 echo"$gender $sercountry<br>";
 
 $query="SELECT *FROM login  ";
    $result=mysql_query($query);
 $num_results=mysql_num_rows($result);
 if($num_results==0){
 echo"<font color=maroon><b>No matches for your search.<br>  specify  category</b><br> <a href=index.php><< BACK</a></font>";
 }
 if($result){
 
 while($row=mysql_fetch_array($result)) { 
      $userid=$row['id'];  
    $name=$row['name']; 
	$user=$row['user']; 
	$family=$row['family']; 
   $country=$row['country']; 
	$date=$row['date']; 
	$gender=$row['gender'];
	
	
	echo "<table width=540 height=20 border=1 bordercolor=cyan cellpadding=5 cellspacing=2 align=center bgcolor=aliceblue>";
 echo"<tr><td width=10%><p><a href= profile.php?wewe=$userid><img src=retrieve.php?id=$userid width=200 height=150 border=0></a></p></td><td width=30%><p>NAME:<font color=blue><a href= profile.php?wewe=$userid> $name</a></font></p>
   <p>FAMILY: <font color=blue>$family</font></p><p>COUNTRY:<font color=blue>$country</font></p><p>GENDER: <font color=blue>$gender</font></p>
     <p>USER: <font color=blue>$user</font></p></td></tr>";
echo "</table>";
	
	}
 
 
 
 
 }
 
 
 
 
exit();
 }
 
if($gender ==all||$sercountry!==all){
echo"$gender $sercountry<br>";
 
 $query="SELECT FROM login where country  like '%$sercountry%'";
    $result=mysql_query($query);
 $num_results=mysql_num_rows($result);
 
 if($num_results==0){
 echo"<font color=maroon><b>No matches for your search.<br>  specify  category</b><br> <a href=index.php><< BACK</a></font>";
 exit();
 }
 
 if($result){
 
 while($row=mysql_fetch_array($result)) { 
      $userid=$row['id'];  
    $name=$row['name']; 
	$user=$row['user']; 
	$family=$row['family']; 
   $country=$row['country']; 
	$date=$row['date']; 
	$gender=$row['gender'];

	echo "<table width=540 height=20 border=1 bordercolor=cyan cellpadding=5 cellspacing=2 align=center bgcolor=aliceblue>";
 echo"<tr><td width=10%><p><a href= profile.php?id=$userid><img src=retrieve.php?id=$userid width=200 height=150 border=0></a></p></td><td width=30%><p>NAME:<font color=blue><a href= profile.php?prof=$userid> $name</a></font></p>
   <p>FAMILY: <font color=blue>$family</font></p><p>COUNTRY:<font color=blue>$country</font></p><p>GENDER: <font color=blue>$gender</font></p>
     <p>USER: <font color=blue>$user</font></p></td></tr>";
echo "</table>";
	
	}
 
 
 
 
 }
 
 
 
 
 
 

exit();
 }
if($gender==p||$sercountry==all){
 echo"$gender $sercountry";
 
 $query="SELECT FROM login where gender='p'";
    $result=mysql_query($query);
 $num_results=mysql_num_rows($result);
 
 if($num_results==0){
 echo"<font color=maroon><b>No matches for your search.<br>  specify  category</b><br> <a href=index.php><< BACK</a></font>";
 exit();
 }
 
 if($result){
 
 while($row=mysql_fetch_array($result)) { 
      $userid=$row['id'];  
    $name=$row['name']; 
	$user=$row['user']; 
	$family=$row['family']; 
   $country=$row['country']; 
	$date=$row['date']; 
	$gender=$row['gender'];

	echo "<table width=540 height=20 border=1 bordercolor=cyan cellpadding=5 cellspacing=2 align=center bgcolor=aliceblue>";
 echo"<tr><td width=10%><p><a href= profile.php?id=$userid><img src=retrieve.php?id=$userid width=200 height=150 border=0></a></p></td><td width=30%><p>NAME:<font color=blue><a href= profile.php?prof=$userid> $name</a></font></p>
   <p>FAMILY: <font color=blue>$family</font></p><p>COUNTRY:<font color=blue>$country</font></p><p>GENDER: <font color=blue>$gender</font></p>
     <p>USER: <font color=blue>$user</font></p></td></tr>";
echo "</table>";
	
	}
 
 
 
 
 }
 
 
 
 
 exit();
 
 }
 
 if($gender==p||$sercountry!==all){
 echo"$gender $sercountry";
 
 $query="SELECT FROM login where gender='p' and country  like '%$sercountry%'";
    $result=mysql_query($query);
 $num_results=mysql_num_rows($result);
 
 if($num_results==0){
 echo"<font color=maroon><b>No matches for your search.<br>  specify  category</b><br> <a href=index.php><< BACK</a></font>";
 exit();
 }
 
 if($result){
 
 while($row=mysql_fetch_array($result)) { 
      $userid=$row['id'];  
    $name=$row['name']; 
	$user=$row['user']; 
	$family=$row['family']; 
   $country=$row['country']; 
	$date=$row['date']; 
	$gender=$row['gender'];

	echo "<table width=540 height=20 border=1 bordercolor=cyan cellpadding=5 cellspacing=2 align=center bgcolor=aliceblue>";
 echo"<tr><td width=10%><p><a href= profile.php?id=$userid><img src=retrieve.php?id=$userid width=200 height=150 border=0></a></p></td><td width=30%><p>NAME:<font color=blue><a href= profile.php?prof=$userid> $name</a></font></p>
   <p>FAMILY: <font color=blue>$family</font></p><p>COUNTRY:<font color=blue>$country</font></p><p>GENDER: <font color=blue>$gender</font></p>
     <p>USER: <font color=blue>$user</font></p></td></tr>";
echo "</table>";
	
	}
 
 
 
 
 }
 
 
 
 
 
 exit();
 
 }
 if($gender==k||$sercountry==all){
 echo"$gender $sercountry";
 
 $query="SELECT FROM login where gender='k'";
    $result=mysql_query($query);
 $num_results=mysql_num_rows($result);
 
 if($num_results==0){
 echo"<font color=maroon><b>No matches for your search.<br>  specify  category</b><br> <a href=index.php><< BACK</a></font>";
 exit();
 }
 
 if($result){
 
 while($row=mysql_fetch_array($result)) { 
      $userid=$row['id'];  
    $name=$row['name']; 
	$user=$row['user']; 
	$family=$row['family']; 
   $country=$row['country']; 
	$date=$row['date']; 
	$gender=$row['gender'];

	echo "<table width=540 height=20 border=1 bordercolor=cyan cellpadding=5 cellspacing=2 align=center bgcolor=aliceblue>";
 echo"<tr><td width=10%><p><a href= profile.php?id=$userid><img src=retrieve.php?id=$userid width=200 height=150 border=0></a></p></td><td width=30%><p>NAME:<font color=blue><a href= profile.php?prof=$userid> $name</a></font></p>
   <p>FAMILY: <font color=blue>$family</font></p><p>COUNTRY:<font color=blue>$country</font></p><p>GENDER: <font color=blue>$gender</font></p>
     <p>USER: <font color=blue>$user</font></p></td></tr>";
echo "</table>";
	
	}
 
 
 
 
 }
 
 
 
 
 
 
 
 exit();
 
 }
 
 if($gender==k||$sercountry!==all){
 echo"$gender $sercountry";
 
 
 $query="SELECT FROM login where gender='k' and country  like '%$sercountry%'";
    $result=mysql_query($query);
 $num_results=mysql_num_rows($result);
 
 if($num_results==0){
 echo"<font color=maroon><b>No matches for your search.<br>  specify  category</b><br> <a href=index.php><< BACK</a></font>";
 exit();
 }
 
 if($result){
 
 while($row=mysql_fetch_array($result)) { 
      $userid=$row['id'];  
    $name=$row['name']; 
	$user=$row['user']; 
	$family=$row['family']; 
   $country=$row['country']; 
	$date=$row['date']; 
	$gender=$row['gender'];

	echo "<table width=540 height=20 border=1 bordercolor=cyan cellpadding=5 cellspacing=2 align=center bgcolor=aliceblue>";
 echo"<tr><td width=10%><p><a href= profile.php?id=$userid><img src=retrieve.php?id=$userid width=200 height=150 border=0></a></p></td><td width=30%><p>NAME:<font color=blue><a href= profile.php?prof=$userid> $name</a></font></p>
   <p>FAMILY: <font color=blue>$family</font></p><p>COUNTRY:<font color=blue>$country</font></p><p>GENDER: <font color=blue>$gender</font></p>
     <p>USER: <font color=blue>$user</font></p></td></tr>";
echo "</table>";
	
	}
 
 
 
 
 }
 
 
 
 
 
 exit();
 
 }
	
	
	?>

Dani AI

Generated

The search failures come down to three practical things: how the form values are checked, how the SQL is built, and the API used to run queries. was right to flag string comparisons and presentation; correctly spotted the SQL syntax slip. Fixing those will get the basic search working. After that, harden the flow: validate inputs, build the WHERE clause only for active filters, and use parameterized queries.

Quick debugging checklist

  • Turn on errors (E_ALL) and inspect the received POST data (var_dump/print_r).
  • Make sure you assign the trimmed values back (trim returns a value).
  • Treat the "all" option by not adding any condition for that field. That avoids awkward OR/AND logic.
  • Test the final SQL in the DB console before running it from PHP.

A simple, modern pattern (use PDO or mysqli prepared statements) is: build an array of conditions, push corresponding parameters, implode with " AND " only when there are conditions, then prepare/execute. This avoids leading/trailing ANDs and injection risks.

// example pattern (PDO)
$where = [];
$params = [];

if ($gender !== 'all') {
    $where[] = 'gender = ?';
    $params[] = $gender;
}
if ($country !== 'all') {
    $where[] = 'country LIKE ?';
    $params[] = "%$country%";
}

$sql = 'SELECT id,name,user,family,country,gender FROM login' . (count($where) ? ' WHERE '.implode(' AND ', $where) : '') . ' LIMIT 100';
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

Notes and cautions: stop using the old mysql_* functions (they are removed in modern PHP). Use prepared statements to prevent SQL injection and escape output (htmlspecialchars) when printing. For reference on prepared statements see PDO prepared statements.

Recommended Answers

All 7 Replies

Here's a few mistakes in your code that I have spotted:

if($gender==all||$sercountry==all){
//should be:
if($gender=="all"||$sercountry=="all"){

Here is how I would structure it:

<?php
include"config.php";

$gender=$_POST['gender'];
$sercountry=$_POST['country'];
trim($gender);
trim($sercountry);

if(!$sercountry || !$gender){ //fail
    echo "<font color=brown><b>You have not entered ALL search details.<b></font>try again</b></font>";
    exit;
}	 
	
if($gender=="all" || $sercountry=="all"){
    echo"$gender $sercountry<br>"; //for debugging
    $query="SELECT *FROM login";
    $result=mysql_query($query);
} else {
    echo"$gender $sercountry<br>"; //for debugging
    $query="SELECT *FROM login WHERE ";
    if($gender != "all"){
        $query .= "gender = `$gender` ";
    }
    if($sercountry != "all"){
        $query .= "country like `$sercountry`";
    }
    $result=mysql_query($query);
}

$num_results=mysql_num_rows($result);
if($num_results==0){
    echo"<font color=maroon><b>No matches for your search.<br>  specify  category</b><br> <a href=index.php><< BACK</a></font>";
}

if($result){
    //this should be out here or you will get a table for each result
    echo "<table width=540 height=20 border=1 bordercolor=cyan cellpadding=5 cellspacing=2 align=center bgcolor=aliceblue>";
    //this is for the table headings
    echo "<tr><td>&nbsp;</td><td>NAME:</td><td>FAMILY:</td><td>COUNTRY:</td><td>GENDER:</td><td>USER:</td></tr>";
    while($row=mysql_fetch_array($result)){
        $userid=$row['id'];  
        $name=$row['name']; 
        $user=$row['user']; 
        $family=$row['family']; 
        $country=$row['country']; 
        $date=$row['date']; 
        $gender=$row['gender'];
        echo"<tr><td width=10%><a href= profile.php?wewe=$userid><img src=retrieve.php?id=$userid width=200 height=150 border=0></a></td><td width=30%><font color=blue><a href= profile.php?wewe=$userid> $name</a></font></td><td><font color=blue>$family</font></td><td><font color=blue>$country</font></td><td><font color=blue>$gender</font></td><td><font color=blue>$user</font></td></tr>";
    }
    echo "</table>";
 }
?>

Because I'm feeling generous, I wrote out the echo big as well. It will now display every result on one line of a table with the headings "&nbsp;", "NAME:", "FAMILY:", "COUNTRY:", "GENDER:" and "USER:".

the queries on lines 16 and 20 will fail because your syntax is wrong (probably i simple typing error).

it should read

$query = "SELECT * FROM `login`";

the queries on lines 16 and 20 will fail because your syntax is wrong (probably i simple typing error).

it should read

$query = "SELECT * FROM `login`";

Are the "`" quotation things actually necessary? www.w3schools.com doesn't mention it and I couldn't see it in 's documentation. Are they only for table names?

Cheers.

no they aren't. what was really trying to show is that you forgot a space between the * and FROM. i wrote those out of habit. i read they help with performance and you see things like phpMyAdmin use them a lot.

they are used around database, table, column names ect.

commented: cheers +4

Oh, ok. didn't even see that. Thanks.

Do you mean that in my code the error is the

if($gender==all||$sercountry==all){
//should be:
if($gender=="all"||$sercountry=="all"){

??
becuse i tried urs but it does not work as i explained eather.
Is no another way of writing without using the else statement you used there?
because i dont know what is wrong with your code,its like everythin is okey but not working))

Is it throwing any errors? Is it echoing anything at all? Make sure the values that are sent to the page via POST are the correct values to use in the mysql table. Note that the 'if $sercounty == "all"' is case sensitive.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.