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();
 
 }
	
	
	?>

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 www.dev.mysql.com'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.