Jump to content

Problem of member searching php code.


tapantor24

Recommended Posts

Look at this code. This is for member search. Hear are sex, district, ,, age search function but it's only work sex function. I try to fix it but I can't. How can i fix this problem?

 

<?php

//allow sessions to be passed so we can see if the user is logged in

session_start();

 

//connect to the database so we can check, edit, or insert data to our users table

include "./include/database.php";

 

//include out functions file giving us access to the protect() function made earlier

include "./functions.php";

 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<!-- TemplateBeginEditable name="doctitle" -->

<title>Bangladesh Activist Forum</title>

<meta name="keywords" content="Activist Forum">

<meta name="keywords" content="Helpline">

<meta name="keywords" content="Friend ship">

<meta name="description" content="Bangladesh Activist Forum">

<!-- TemplateEndEditable -->

<!-- TemplateBeginEditable name="head" -->

<!-- TemplateEndEditable -->

<link href="css/styles.css" rel="stylesheet" type="text/css">

</head>

 

<body>

<div class="con2">

<!-- start. header -->

<?php include("./include/header.php"); ?>

<!-- end. header -->

<!-- start. searce_bar -->

<?php include("./include/searce_bar.php"); ?>

<!-- end. searce_bar -->

<br/>

<div class="container">

<!-- sidebar1 start -->

<?php include("./include/side_bar1.php"); ?>

<!-- end .sidebar1 -->

 

<div class="content">

<h1> Your Search result..</h1>

<?php

 

if(isset($_POST['sex']))

{

$sex = $_POST['sex'];

}elseif(isset($_GET['sex']))

{

$sex = $_GET['sex'];

}

 

if(isset($_POST['district']))

{

$district = $_POST['district'];

}elseif(isset($_GET['district']))

{

$district = $_GET['district'];

}

 

if(isset($_POST['age1']))

{

$age1 = ($_POST['age1']);

}elseif(isset($_GET['age1']))

{

$age1 = ($_GET['age1']);

}

 

if(isset($_POST['age2']))

{

$age2 = ($_POST['age2']);

}elseif(isset($_GET['age2']))

{

$age2 = ($_GET['age2']);

}

 

 

//protect the posted value then store them to variables

 

//$name = protect($_POST['fname']);

 

 

 

 

 

//$sex = $_POST['sex'];

//$merital = $_POST['merital'];

//$district = $_POST['district'];

//$age1 = protect($_POST['age1']);

//$age2 = protect($_POST['age2']);

$perpage = 6;

 

if(isset($_GET["page"]))

 

{

 

$page = intval($_GET["page"]);

 

}

 

else

 

{

 

$page = 1;

 

}

 

$calc = $perpage * $page;

 

$start = $calc - $perpage;

if ($sex == 'All') {

$result = mysql_query("select * from member where (Sex = 'Male') || (Sex = 'Female') AND (Age BETWEEN '$age1' AND '$age2') || (District = '$district') Limit $start, $perpage");

} else if ($sex == 'Female') {

$result = mysql_query("select * from member where (Sex = 'Female') AND (Age BETWEEN '$age1' AND '$age2') || (District = '$district') Limit $start, $perpage");

} else {

$result = mysql_query("select * from member where (Sex = 'Male') AND (Age BETWEEN '$age1' AND '$age2') || (District = '$district') Limit $start, $perpage");

}

$rows = mysql_num_rows($result);

if($rows){

$i=0;

?>

 

<?php

 

while($row= mysql_fetch_array($result))

{

//$row['Photo'] = "<img src="/user_image/ . $row['Photo']; ""/>

if ($i%2) {

$class = 'row1';

} else {

$class = 'row2';

}

 

$i += 1;

$name=$row['FName']." ".$row['LName'];

?>

 

 

 

 

 

 

<table width="100%">

 

 

 

 

 

<tr width="150" class="label">

 

<td width="173" rowspan="3"> <div align="center">

 

 

<?php

if ($row['pd_image'] !='') {

?>

<img src="image/product/<?php echo $row['pd_image']; ?>" width="120 height="118"/>

<?php

} else {?>

 

<img src="image/no-image-small.png" width="120" height="118"/>

<?php } ?>

 

 

 

 

 

 

 

 

 

</div></td>

<td height="21" colspan="2" >

<div align="center" style="font:'Times New Roman', Times, serif; font-size:15px;">

<div align="left">Name:

<?php if($name != '') { ?>

<?php echo $name; ?>

<?php }else {

 

echo "N/A";

 

 

} ?>

</div>

</div></td>

</tr> <tr width="150" class="label">

 

<td height="21" colspan="2" > <div align="center" style="font:'Times New Roman', Times, serif; font-size:15px;">

<div align="left">Profession:

<?php if($row['position'] != '') {

 

?>

<?php echo $row['position']; ?>

<?php }else {

 

echo "N/A";

 

 

} ?>

</div>

</div></td>

</tr>

<tr width="150" class="label">

 

<td height="21" colspan="2" > <div align="center" style="font:'Times New Roman', Times, serif; font-size:15px;">

<div align="left">Sex:

<?php echo $row['Sex']; ?>

 

</div>

</div>

<div align="center" style="font:'Times New Roman', Times, serif; font-size:15px;">

<div align="left">District:

<?php echo $row['District']; ?>

 

</div>

</div>

<div align='right'><a href="detail.php?Id=<?php echo $row['ID'];?>"> View</a></div>

 

</td>

 

 

 

 

 

 

 

</tr>

 

</table>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<?php

}

?>

 

<?php

}

else {

 

print "<p align='center'>Not find any user on you search. Please try again.</p>";

 

 

}

 

 

//$count++

/** while ($row = mysql_fetch_array($sql)){

echo 'ID: '.$row['ID'];

echo '<br/> First Name: '.$row['FName'];

echo '<br/> Last Name: '.$row['LName'];

echo '<br/> Phone: '.$row['Phone'];

echo '<br/><br/>';

} **/

 

 

?>

 

<table width="100%" cellspacing="2" cellpadding="2" align="center" >

 

<tr>

 

<td align="center">

 

<?php

 

if(isset($page))

 

{

if ($sex == 'All') {

$result = mysql_query("select Count(*) As Total from member where (Sex = 'Male') || (Sex = 'Female') AND (Age BETWEEN '$age1' AND '$age2') || (District = '$district') ");

 

 

$rows = mysql_num_rows($result);

 

if($rows)

 

{

 

$rs = mysql_fetch_array($result);

 

$total = $rs["Total"];

 

}

 

$totalPages = ceil($total / $perpage);

 

if($page <=1 )

 

{

 

echo "<span id='page_links' style='font-weight:bold;'>Prev</span>";

 

}

 

else

 

{

 

$j = $page - 1;

 

echo "<span><a id='page_a_link' href='search.php?page=$j&sex=$sex&District=$district&Age=$age1&Age=$age2'>< Prev</a></span>";

 

}

 

for($i=1; $i <= $totalPages; $i++)

 

{

 

if($i<>$page)

 

{

 

echo "<span><a href='search.php?page=$i&sex=$sex&District=$district&Age=$age1&Age=$age2' id='page_a_link'>$i</a></span>";

 

}

 

else

 

{

 

echo "<span id='page_links' style='font-weight:bold;'>$i</span>";

 

}

 

}

 

if($page == $totalPages )

 

{

 

echo "<span id='page_links' style='font-weight:bold;'>Next ></span>";

 

}

 

else

 

{

 

$j = $page + 1;

 

echo "<span><a href='search.php?page=$j&sex=$sex&District=$district&Age=$age1&Age=$age2' id='page_a_link'>Next</a></span>";

 

}

 

}

} else if ($sex == 'Female') {

$result = mysql_query("select Count(*) As Total from member where (Sex = 'Female') AND (Age BETWEEN '$age1' AND '$age2') || (District = '$district') ");

 

$rows = mysql_num_rows($result);

 

if($rows)

 

{

 

$rs = mysql_fetch_array($result);

 

$total = $rs["Total"];

 

}

 

$totalPages = ceil($total / $perpage);

 

if($page <=1 )

 

{

 

echo "<span id='page_links' style='font-weight:bold;'>Prev</span>";

 

}

 

else

 

{

 

$j = $page - 1;

 

echo "<span><a id='page_a_link' href='search.php?page=$j&sex=$sex&District=$district&Age=$age1&Age=$age2'>< Prev</a></span>";

 

}

 

for($i=1; $i <= $totalPages; $i++)

 

{

 

if($i<>$page)

 

{

 

echo "<span><a href='search.php?page=$i&sex=$sex&District=$district&Age=$age1&Age=$age2' id='page_a_link'>$i</a></span>";

 

}

 

else

 

{

 

echo "<span id='page_links' style='font-weight:bold;'>$i</span>";

 

}

 

}

 

if($page == $totalPages )

 

{

 

echo "<span id='page_links' style='font-weight:bold;'>Next ></span>";

 

}

 

else

 

{

 

$j = $page + 1;

 

echo "<span><a href='search.php?page=$j&sex=$sex&District=$district&Age=$age1&Age=$age2' id='page_a_link'>Next</a></span>";

 

}

 

 

} else {

$result = mysql_query("select Count(*) As Total from member where (Sex = 'Male') AND (Age BETWEEN '$age1' AND '$age2') || (District = '$district') ");

 

$rows = mysql_num_rows($result);

 

if($rows)

 

{

 

$rs = mysql_fetch_array($result);

 

$total = $rs["Total"];

 

}

 

$totalPages = ceil($total / $perpage);

 

if($page <=1 )

 

{

 

echo "<span id='page_links' style='font-weight:bold;'>Prev</span>";

 

}

 

else

 

{

 

$j = $page - 1;

 

echo "<span><a id='page_a_link' href='search.php?page=$j&sex=$sex&District=$district&Age=$age1&Age=$age2'>< Prev</a></span>";

 

}

 

for($i=1; $i <= $totalPages; $i++)

 

{

 

if($i<>$page)

 

{

 

echo "<span><a href='search.php?page=$i&sex=$sex&District=$district&Age=$age1&Age=$age2' id='page_a_link'>$i</a></span>";

 

}

 

else

 

{

 

echo "<span id='page_links' style='font-weight:bold;'>$i</span>";

 

}

 

}

 

if($page == $totalPages )

 

{

 

echo "<span id='page_links' style='font-weight:bold;'>Next ></span>";

 

}

 

else

 

{

 

$j = $page + 1;

 

echo "<span><a href='search.php?page=$j&sex=$sex&District=$district&Age=$age1&Age=$age2' id='page_a_link'>Next</a></span>";

 

}

 

}

?>

 

<td>

 

</tr>

 

</table>

 

 

 

 

 

 

 

<!-- end .content --></div>

<!-- sidebar2 start -->

<?php include("./include/side_bar2.php"); ?>

<!-- end .sidebar2 -->

 

<!-- end .contain"er --></div>

<!-- start.footer -->

<?php include("./include/footer.php"); ?>

<!-- end .footer -->

</div>

</body>

</html>

Link to comment
Share on other sites

Would you really like to have to go through miles of someone's code to help them with a query problem?  I don't.  How about isolating your own problem and displaying the query before you run it and then verifying that it ran with a check and then asking us to look at just that much of your code?

Link to comment
Share on other sites

Would you really like to have to go through miles of someone's code to help them with a query problem?  I don't.  How about isolating your own problem and displaying the query before you run it and then verifying that it ran with a check and then asking us to look at just that much of your code?

... you forgot to mention the total lack of indentation and formatting to make it readable (just in case anyone wanted to)

Link to comment
Share on other sites

Dear friend, This is running code of my site www.bd5000.com where visitor can search the member. If you will see the search form you will see that only seeking search working and another district and age are not working. I read many topics and many tutorial to solv this problem. Where the problem I don't understand. If you caught the problem please tell me where I miss take. Thanks

Link to comment
Share on other sites

no one here is going to try and figure out your code the way it is written or posted. if you fix the following things in your code, so that you or anyone else can see just what your code actually is, and repost it using the forum's 


bbcode tags (the edit form's <> button), someone might take the time to look at it -

 

1) remove all the html comments associated with the template you are using or where you got this page from. html comments are rather pointless when dealing with a dynamically produced page.

 

2) remove all the commented out php code. if the code's not in use, it doesn't do you or us any good to have it clutter up what the actual code is.

 

3) remove all the excessive empty lines. you don't need more than one blank line between different sections of code to set them apart.

 

4) every place where you have a closing php tag, immediately followed by an opening php tag, remove those. there's no point to it.

 

5) indent your code so that statements at the same block level are indented the same amount.

 

 

lastly, some comments about your actual code -

 

1) Don't Repeat Yourself (DRY). you have code repeated three times, that only differs in a value. you need to factor out the things that are different, then use one set of common code.

 

2) you need to use method='get' for your search form. what you are doing is specifying what is to be retrieved (gotten, i.e. GET) on a page. this will eliminate all your $_POST logic.

 

3) you need to move your count(*) query logic to be near the start of the php code so that you can use the total page count to limit the $_GET['page'] value.

 

4) your count(*) query needs to have the same WHERE clause as your main data retrieval query. i.e. you should be building the WHERE clause once, in a php variable, then use that in all the queries.

 

5) you need to learn how to use the ternary operator - (expr1) ? (expr2) : (expr3) to simplify code.

 

6) to retrieve ALL matching values for a column, just leave that column out of the WHERE clause in the query.

 

7) you need to validate and/or escape all external values being put into your query statements.

 

that should be enough to get you started. i think that by completing item #4 in this second list, that you will be able to see what is wrong with your code that is causing it to not use the search terms the way you want.

Link to comment
Share on other sites

Hi friends, This is the member searching code which I use in my client's site www.bd5000.com It's have

Seaking:

District:

Age:

but it's only work on Seaking search function. If you see the form you will understand your self.

Please see the code and tell me what should fixing?

The code is::

  <?php

	if(isset($_POST['sex']))
{
$sex = $_POST['sex'];
}elseif(isset($_GET['sex']))
{
$sex = $_POST['sex'];
} 

	if(isset($_POST['district']))
{
$district = $_POST['district'];
}elseif(isset($_GET['district']))
{
$district = $_POST['district'];
} 
	 
	 	if(isset($_POST['age1']))
{
$age1 = ($_POST['age1']);
}elseif(isset($_GET['age1']))
{
$age1 = ($_POST['age1']);
} 

	 	if(isset($_POST['age2']))
{
$age2 = ($_POST['age2']);
}elseif(isset($_GET['age2']))
{
$age2 = ($_POST['age2']);
} 
$perpage = 5;
	 
	if(isset($_GET["page"]))
	 
	{
	 
	$page = intval($_GET["page"]);
	 
	}
	 
	else
	 
	{
	 
	$page = 1;
	 
	}
	 
	$calc = $perpage * $page;
	 
	$start = $calc - $perpage;
if ($sex == 'All') {
$result = mysql_query("select * from member where  (Sex = 'Male') || (Sex = 'Female') AND (Age BETWEEN  '$age1' AND  '$age2') ||  (District = '$district') Limit $start, $perpage");
} else if ($sex == 'Female') {
$result = mysql_query("select * from member where  (Sex = 'Female')  AND (Age BETWEEN  '$age1' AND  '$age2') ||  (District = '$district') Limit $start, $perpage");
 } else {
 $result = mysql_query("select * from member where  (Sex = 'Male')  AND (Age BETWEEN  '$age1' AND  '$age2') ||  (District = '$district') Limit $start, $perpage");
}
 $rows = mysql_num_rows($result);
 if($rows){
 $i=0;
?>
 
<?php

while($row= mysql_fetch_array($result))
{
	//$row['Photo'] = "<img src="/user_image/ . $row['Photo']; ""/>
	if ($i%2) {
			$class = 'row1';
		} else {
			$class = 'row2';
		}
		
		$i += 1;
		$name=$row['FName']." ".$row['LName'];
?>

<table width="100%"> 
          
  <tr width="150" class="label">
                             
    <td width="173" rowspan="3"> <div align="center">
   
<?php
  if ($row['pd_image'] !='') {
  ?>
    <img src="image/product/<?php echo $row['pd_image']; ?>" width="120 height="118"/>
 <?php
 } else {?>

  <img src="image/no-image-small.png" width="120" height="118"/>
<?php } ?> 
       			
    </div></td>
<td height="21" colspan="2" > 
<div align="center" style="font:'Times New Roman', Times, serif; font-size:15px;">
                                <div align="left">Name: 
                                  <?php if($name != '') { ?>
							      <?php echo $name; ?>
						          <?php }else {
						
						echo "N/A";
						 
							
						 } ?>
                                </div>
              </div></td>
          </tr> <tr width="150" class="label">
                          
                              <td height="21" colspan="2" > <div align="center" style="font:'Times New Roman', Times, serif; font-size:15px;">
                                <div align="left">Profession:  
                                  <?php if($row['position'] != '') { 
								 
								  ?>
							      <?php echo $row['position']; ?>
						          <?php }else {
						
						echo "N/A";
						 
							
						 } ?> 
                                </div>
                              </div></td>
                            </tr> 
							<tr width="150" class="label">
                          
                              <td height="21" colspan="2" > <div align="center" style="font:'Times New Roman', Times, serif; font-size:15px;">
                                <div align="left">Sex:  
                                   <?php echo $row['Sex']; ?> 
								    
                                </div>
                              </div>
							   <div align="center" style="font:'Times New Roman', Times, serif; font-size:15px;">
                                <div align="left">District:  
                                   <?php echo $row['District']; ?> 
								    
                                </div>
                              </div>
							                       <div align='right'><a href="detail.php?Id=<?php echo $row['ID'];?>"> View</a></div>

							  </td>
											  
                            </tr> 
							
                            </table>
 
 <?php 
 }
 ?>

<?php
}
else {

print "<p align='center'>Not find any user on you search. Please try again.</p>";


}
?>
<table width="100%" cellspacing="2" cellpadding="2" align="center" >
	 
	<tr>
	 
	<td align="center">
	 
	<?php
	 
	if(isset($page))
	 
	{
if ($sex == 'All') {
$result = mysql_query("select Count(*) As Total from member where  (Sex = 'Male') || (Sex = 'Female') AND (Age BETWEEN  '$age1' AND  '$age2') ||  (District = '$district') ");


	$rows = mysql_num_rows($result);
	 
	if($rows)
	 
	{
	 
	$rs = mysql_fetch_array($result);
	 
	$total = $rs["Total"];
	 
	}
	 
	$totalPages = ceil($total / $perpage);
	 
	if($page <=1 )
	 
	{
	 
	echo "<span id='page_links' style='font-weight:bold;'>Prev</span>";
	 
	}
	 
	else
	 
	{
	 
	$j = $page - 1;
	 
	echo "<span><a id='page_a_link' href='search.php?page=$j&sex=$sex&District=$district&Age=$age1&Age=$age2'>< Prev</a></span>";
	 
	}
	 
	for($i=1; $i <= $totalPages; $i++)
	 
	{
	 
	if($i<>$page)
	 
	{
	 
	echo "<span><a href='search.php?page=$i&sex=$sex&District=$district&Age=$age1&Age=$age2' id='page_a_link'>$i</a></span>";
	 
	}
	 
	else
	 
	{
	 
	echo "<span id='page_links' style='font-weight:bold;'>$i</span>";
	 
	}

	}
	 
	if($page == $totalPages )
	 
	{
	 
	echo "<span id='page_links' style='font-weight:bold;'>Next ></span>";
	 
	}
	 
	else
	 
	{
	 
	$j = $page + 1;
	 
	echo "<span><a href='search.php?page=$j&sex=$sex&District=$district&Age=$age1&Age=$age2' id='page_a_link'>Next</a></span>";
	 
	}
	 
	}
	 } else if ($sex == 'Female') {
$result = mysql_query("select Count(*) As Total from member where  (Sex = 'Female')  AND (Age BETWEEN  '$age1' AND  '$age2') ||  (District = '$district') ");
 
 $rows = mysql_num_rows($result);
	 
	if($rows)
	 
	{
	 
	$rs = mysql_fetch_array($result);
	 
	$total = $rs["Total"];
	 
	}
	 
	$totalPages = ceil($total / $perpage);
	 
	if($page <=1 )
	 
	{
	 
	echo "<span id='page_links' style='font-weight:bold;'>Prev</span>";
	 
	}
	 
	else
	 
	{
	 
	$j = $page - 1;
	 
	echo "<span><a id='page_a_link' href='search.php?page=$j&sex=$sex&District=$district&Age=$age1&Age=$age2'>< Prev</a></span>";
	 
	}
	 
	for($i=1; $i <= $totalPages; $i++)
	 
	{
	 
	if($i<>$page)
	 
	{
	 
	echo "<span><a href='search.php?page=$i&sex=$sex&District=$district&Age=$age1&Age=$age2' id='page_a_link'>$i</a></span>";
	 
	}
	 
	else
	 
	{
	 
	echo "<span id='page_links' style='font-weight:bold;'>$i</span>";
	 
	}

	}
	 
	if($page == $totalPages )
	 
	{
	 
	echo "<span id='page_links' style='font-weight:bold;'>Next ></span>";
	 
	}
	 
	else
	 
	{
	 
	$j = $page + 1;
	 
	echo "<span><a href='search.php?page=$j&sex=$sex&District=$district&Age=$age1&Age=$age2' id='page_a_link'>Next</a></span>";
	 
	}
 
 
 } else {
 $result = mysql_query("select Count(*) As Total from member where  (Sex = 'Male')  AND (Age BETWEEN  '$age1' AND  '$age2') ||  (District = '$district') ");

 $rows = mysql_num_rows($result);
	 
	if($rows)
	 
	{
	 
	$rs = mysql_fetch_array($result);
	 
	$total = $rs["Total"];
	 
	}
	 
	$totalPages = ceil($total / $perpage);
	 
	if($page <=1 )
	 
	{
	 
	echo "<span id='page_links' style='font-weight:bold;'>Prev</span>";
	 
	}
	 
	else
	 
	{
	 
	$j = $page - 1;
	 
	echo "<span><a id='page_a_link' href='search.php?page=$j&sex=$sex&District=$district&Age=$age1&Age=$age2'>< Prev</a></span>";
	 
	}
	 
	for($i=1; $i <= $totalPages; $i++)
	 
	{
	 
	if($i<>$page)
	 
	{
	 
	echo "<span><a href='search.php?page=$i&sex=$sex&District=$district&Age=$age1&Age=$age2' id='page_a_link'>$i</a></span>";
	 
	}
	 
	else
	 
	{
	 
	echo "<span id='page_links' style='font-weight:bold;'>$i</span>";
	 
	}

	}
	 
	if($page == $totalPages )
	 
	{
	 
	echo "<span id='page_links' style='font-weight:bold;'>Next ></span>";
	 
	}
	 
	else
	 
	{
	 
	$j = $page + 1;
	 
	echo "<span><a href='search.php?page=$j&sex=$sex&District=$district&Age=$age1&Age=$age2' id='page_a_link'>Next</a></span>";
	 
	}
 
 }	
	?>
	 
	<td>
 
	</tr>
	 
	</table>

Link to comment
Share on other sites

I see three big issues with the code at a glance:

 

First, you use $_POST whether the value is set with the POST or GET method. For example:

	if(isset($_POST['sex']))
{
$sex = $_POST['sex'];
}elseif(isset($_GET['sex']))
{
$sex = $_POST['sex'];
} 

Then you don't sanitize the input, leaving yourself open to an SQL injection attack. You should do something like:

	if(isset($_POST['sex']))
{
$sex = mysqli_real_escape_string($link,$_POST['sex']);

Finally, you are using || in your queries where you should use &&. If I am looking for a girl in District 5 both being a girl and being in District 5 must be true (using an or gives me all girls and all people in district 5). You also need to omit from the query any fields on which the user is not searching so if district was left blank you don't only get records with a blank district in the result set.

Link to comment
Share on other sites

District is not blank. If i remove all from seeking and pagination code then it's work fine. I think the problem is hear if ( isset ( $_POST [ 'sex' ]))

{

$sex = $_POST ['sex' ];

} elseif( isset ( $_GET ['sex' ]))

{

$sex = $_POST ['sex' ];

}

if ( isset ( $_POST [ 'district']))

{

$district = $_POST ['district' ];

} elseif( isset ( $_GET ['district' ]))

{

$district = $_POST ['district' ];

}

if ( isset ( $_POST ['age1' ]))

{

$age1 = ( $_POST [ 'age1' ]);

} elseif( isset ( $_GET ['age1' ]))

{

$age1 = ( $_POST [ 'age1' ]);

}

if ( isset ( $_POST ['age2' ]))

{

$age2 = ( $_POST [ 'age2' ]);

} elseif( isset ( $_GET ['age2' ]))

{

$age2 = ( $_POST [ 'age2' ]);

} and in queries. but how i solve it?

Link to comment
Share on other sites

Friends this is the simple php code.

<?php
$dbHost = 'localhost'; // localhost will be used in most cases
// set these to your mysql database username and password.
$dbUser = 'root'; 
$dbPass = '12345';
$dbDatabase = 'member'; // the database you put the table into.
$con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error());

mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());
?>
<?php
if(isset($_POST['submit']))
{
$sex = $_POST['sex'];
$district = $_POST['district'];
$age1 = ($_POST['age1']);
$age2 = ($_POST['age2']);
} elseif(isset($_GET['submit'])) 
{
$sex = $_GET['sex'];
$district = $_GET['district'];
$age1 = ($_GET['age1']);
$age2 = ($_GET['age2']);
}
if ($sex == 'All' && $district == '$district' && $age1 == '$age1' && $age2 == '$age2') {
$result = mysql_query("select * from user where  (Sex = 'Male') AND (Sex = 'Female') AND (Age BETWEEN  '$age1' AND  '$age2') || (District = '$district') ");
} else if ($sex == 'Female' && $district == '$district' && $age1 == '$age1' && $age2 == '$age2') {
$result = mysql_query("select * from user where  (Sex = 'Female')  AND (Age BETWEEN  '$age1' AND  '$age2') ||  (District = '$district')");
 } else {
 $result = mysql_query("select * from user where  (Sex = 'Male')  AND (Age BETWEEN  '$age1' AND  '$age2') ||  (District = '$district')");
}
 $rows = mysql_num_rows($result);
 if($rows){
 $i=0;
?>
 
<?php

while($row= mysql_fetch_array($result))
{
	//$row['Photo'] = "<img src="/user_image/ . $row['Photo']; ""/>
	
?>

<table width="100%"> 

							<tr width="150" class="label">
                          
                              <td height="21" colspan="2" > <div align="center" style="font:'Times New Roman', Times, serif; font-size:15px;">
                                <div align="left">Sex:  
                                   <?php echo $row['Sex']; ?> 
								    
                                </div>
                              </div>
							   <div align="center" style="font:'Times New Roman', Times, serif; font-size:15px;">
                                <div align="left">District:  
                                   <?php echo $row['District']; ?> 
								    
                                </div>
                              </div>

							  </td>
						  
                            </tr> 
							
                            </table>

<?php 
 }}
 ?>

and this is Form code.

<html>
   <title>My Simple Search Form</title>

   <body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">



<tr>
<td><label>Seeking:</label></td>
    <td> <select name="sex" id="sex" class="box" >
			  		<option >All</option>
					<option >Male</option>
					<option >Female</option>
					
					
			  	</select>*<br class="clearBoth" /></td>
                </tr>

                <tr>
                <td><label>District:</label></td>
                <td><select name="district" id="district" class="box" >
     <option >Dhaka</option>
       	
     <option >Bagerhat</option>";
        
        	
     <option >Bandarnban</option>";
        
        	

        
           </select>
           *<br class="clearBoth" /></td>
           </tr>
           <tr>
	 <td><label> Age:</label></td>
	 <td> <select name="age1" id="age1" class="box">
			  		<option >18</option>
					<option >19</option>
					<option >20</option>
					<option >21</option>
					<option >22</option>
					<option >23</option>
					<option >24</option>
					<option >25</option>
					<option >26</option>
					<option >27</option>
					<option >28</option>
					<option >29</option>
					<option >30</option>
					<option >31</option>
					<option >32</option>
					<option >33</option>
					<option >34</option>
					<option >35</option>
			  	</select>* To
				 <select name="age2" id="age2" class="box">
			  		<option >35</option>
					<option >34</option>
					<option >33</option>
					<option >32</option>
					<option >31</option>
					<option >30</option>
					<option >29</option>
					<option >28</option>
					<option >27</option>
					<option >26</option>
					<option >25</option>
					<option >24</option>
					<option >23</option>
					<option >22</option>
					<option >21</option>
					<option >20</option>
					<option >19</option>
					<option >18</option>
			  	</select>*<br class="clearBoth" /></td>
	</tr>

<tr>
<td colspan="2" align="center"><div><input type="submit" name="submit" value="Search!" />
</div></td>
</tr> 
</form>
</table>
   </body>
</html>

Heare everything is working fine but Seaking does not work properly. When I select Male or Female and press submit It show all result, even male and female too.

Link to comment
Share on other sites

Firstly, get rid of those ridiculous if() statements

 

 

if ($sex == 'All' && $district == '$district' && $age1 == '$age1' && $age2 == '$age2')
$age = 18;
echo '$age'; // output the string $age
echo $age;   // output the value 18

So using single quotes the two are never equal. And if it did work, what is the point of testing if 18 = 18?

The only relevant bit is the if($sex == 'All). As you have been told before, if all sexes are required then leave the "Sex" field out of the WHERE clause completely.

 

Another thing you have been told is to use AND instead of OR in the WHERE clause. If you use OR you get all female plus any one in the district regardless of sex.

 

Read the replies and you might get somewhere.

Edited by Barand
Link to comment
Share on other sites

You fundamentally need to understand the difference between AND and OR:

if ($sex == 'All'') {
$result = mysql_query("select * from user where  (Age BETWEEN '$age1' AND '$age2') && (District = '$district') ");
} else if ($sex == 'Female') {
$result = mysql_query("select * from user where (Sex = 'Female') AND (Age BETWEEN '$age1' AND '$age2') && (District = '$district')");
} else {
$result = mysql_query("select * from user where (Sex = 'Male') AND (Age BETWEEN '$age1' AND '$age2') && (District = '$district')");
}

You can't have a record where sex=male and sex=female.

 

Also, as suggested earlier switch to mysqli (mysql is deprecated)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.