Author Topic: Help with search system...  (Read 487 times)

Loka

  • Guest
Help with search system...
« on: August 03, 2010, 08:21:07 PM »
Ok so I made this code that is searching thru a database with names and such, and if you enter a name like john and age 20, but there is no john that is 20 in the database it shows all the johns and so on with age if there is no john it shows all the people that are 20, well I think you get it.

BUT, if all fields aren't filled in with something it shows all the persons in the database, so I would need a little bit of help...

This is the code:

Code: [Select]
if(isset($_POST['screenname'])) {$a = mysql_query("SELECT * FROM Persons WHERE ScreenName LIKE '%$_POST[screenname]%'"); }
if(isset($_POST['firstname'])) {$b = mysql_query("SELECT * FROM Persons WHERE Firstname LIKE '%$_POST[firstname]%'"); }
if(isset($_POST['lastname'])) {$c = mysql_query("SELECT * FROM Persons WHERE LastName LIKE '%$_POST[lastname]%'"); }
if(isset($_POST['age'])) {$d = mysql_query("SELECT * FROM Persons WHERE Age LIKE '%$_POST[age]%'"); }
if(isset($_POST['country'])) {$e = mysql_query("SELECT * FROM Persons WHERE Country LIKE '%$_POST[country]%'"); }
if(isset($_POST['other'])) {$f = mysql_query("SELECT * FROM Persons WHERE Other LIKE '%$_POST[other]%'"); }

while($rowa = mysql_fetch_array($a))
  {
   echo  $rowa['ScreenName'] . " " . $rowa['FirstName'] . " " . $rowa['LastName']. " " . $rowa['Age']. " " . $rowa['Country']. " " . $rowa['Other'];
   echo "<br />";
  }

while($rowb = mysql_fetch_array($b))
  {
   echo  $rowb['ScreenName'] . " " . $rowb['FirstName'] . " " . $rowb['LastName']. " " . $rowb['Age']. " " . $rowb['Country']. " " . $rowb['Other'];
   echo "<br />";
  }

while($rowc = mysql_fetch_array($c))
  {
   echo  $rowc['ScreenName'] . " " . $rowc['FirstName'] . " " . $rowc['LastName']. " " . $rowc['Age']. " " . $rowc['Country']. " " . $rowc['Other'];
   echo "<br />";
  }

while($rowd = mysql_fetch_array($d))
  {
   echo  $rowd['ScreenName'] . " " . $rowd['FirstName'] . " " . $rowd['LastName']. " " . $rowd['Age']. " " . $rowd['Country']. " " . $rowd['Other'];
   echo "<br />";
  }

while($rowe = mysql_fetch_array($e))
  {
   echo  $rowe['ScreenName'] . " " . $rowe['FirstName'] . " " . $rowe['LastName']. " " . $rowe['Age']. " " . $rowe['Country']. " " . $rowe['Other'];
   echo "<br />";
  }

while($rowf = mysql_fetch_array($f))
  {
   echo  $rowf['ScreenName'] . " " . $rowf['FirstName'] . " " . $rowf['LastName']. " " . $rowf['Age']. " " . $rowf['Country']. " " . $rowf['Other'];;
   echo "<br />";
  }

phphelp

  • Administrator
  • Senior Member
  • *****
  • Posts: 777
  • Karma: +3/-0
    • View Profile
    • PHP Help forum
Re: Help with search system...
« Reply #1 on: August 04, 2010, 02:52:45 AM »
Looks like with your code you may have duplicate results displayed - because you make separate query for each of 6 search criteria. Why not combine all your queries into one? Like this:

PHP Code: [Select]

<?php
  $criteria 
"";
  if(isset(
$_POST['screenname'])) $criteria.=($criteria==""?"":" OR ")."ScreenName LIKE '%".mysql_real_escape_string($_POST['screenname'])."%'";
  if(isset(
$_POST['firstname'])) $criteria.=($criteria==""?"":" OR ")."FirstName LIKE '%".mysql_real_escape_string($_POST['firstname'])."%'";
  if(isset(
$_POST['lastname'])) $criteria.=($criteria==""?"":" OR ")."LastName LIKE '%".mysql_real_escape_string($_POST['lastname'])."%'";
  if(isset(
$_POST['age'])) $criteria.=($criteria==""?"":" OR ")."Age LIKE '%".mysql_real_escape_string($_POST['age'])."%'";
  if(isset(
$_POST['country'])) $criteria.=($criteria==""?"":" OR ")."Country LIKE '%".mysql_real_escape_string($_POST['country'])."%'";
  if(isset(
$_POST['other'])) $criteria.=($criteria==""?"":" OR ")."Other LIKE '%".mysql_real_escape_string($_POST['other'])."%'";

  if(
$criteria!=""){
    
$a mysql_query("SELECT * FROM Persons WHERE ".$criteria);
  }
  else{
    die(
'Please specify search criteria!');
  }
?>
Are you experienced in PHP programming and willing to share your knowledge?
Join us at php help forum!

loka123

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Help with search system...
« Reply #2 on: August 04, 2010, 04:23:46 AM »
Well I tried that, but it does not show any results... :/

I just copied the code... Or should I add something to it?

Sorry, I'm not to good with PHP..

phphelp

  • Administrator
  • Senior Member
  • *****
  • Posts: 777
  • Karma: +3/-0
    • View Profile
    • PHP Help forum
Re: Help with search system...
« Reply #3 on: August 04, 2010, 04:41:45 AM »
You also need to add output of query results below, just like you do in your code:

PHP Code: [Select]

while($rowa mysql_fetch_array($a))
  {
   echo  
$rowa['ScreenName'] . " " $rowa['FirstName'] . " " $rowa['LastName']. " " $rowa['Age']. " " $rowa['Country']. " " $rowa['Other'];
   echo 
"<br />";
  }
Are you experienced in PHP programming and willing to share your knowledge?
Join us at php help forum!

loka123

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Help with search system...
« Reply #4 on: August 04, 2010, 04:50:47 AM »
Aha, ok will try it.

Thanks.

EDIT:
God dammit, it still shows all the persons in the database if a field is left blank.
« Last Edit: August 04, 2010, 04:53:18 AM by loka123 »

phphelp

  • Administrator
  • Senior Member
  • *****
  • Posts: 777
  • Karma: +3/-0
    • View Profile
    • PHP Help forum
Re: Help with search system...
« Reply #5 on: August 04, 2010, 08:39:11 AM »
If all fields are left blank - script should now say: Please specify search criteria!
If at least one of fields is filled in ($criteria variable is not empty), then your query will return only results matching the criteria. Depending on criteria entered, it is still possible that all records returned - you need to analyze your data in the Persons table.

Quote
God dammit, it still shows all the persons in the database if a field is left blank.
What field do you mean?
Are you experienced in PHP programming and willing to share your knowledge?
Join us at php help forum!

phphelp

  • Administrator
  • Senior Member
  • *****
  • Posts: 777
  • Karma: +3/-0
    • View Profile
    • PHP Help forum
Re: Help with search system...
« Reply #6 on: August 04, 2010, 08:46:09 AM »
I would also suggest to check form fields like this:

PHP Code: [Select]

if($_POST['screenname']!=''$criteria.=($criteria==""?"":" OR ")."ScreenName LIKE '%".mysql_real_escape_string($_POST['screenname'])."%'";


So, instead of checking variable with isset() function, you need to check if that variable is empty or not. You'll need to do this for each of fields.
Are you experienced in PHP programming and willing to share your knowledge?
Join us at php help forum!

loka123

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Help with search system...
« Reply #7 on: August 04, 2010, 10:29:48 AM »
Well here is the html files that sends the data to the PHP file:

<html>
<head>
<title> Search </title>
</head>
<body BGCOLOR = #000000 TEXT = #FFFFFF>
<center>
<h1>Search</h1>
<p> Enter any info you know about the person you want to search for. </p>
<form action="search-results.php" method="post">
Screen Name: <input type="text" name="screenname" /><br>
First Name: <input type="text" name="firstname" /><br>
Last Name: <input type="text" name="lastname" /><br>
Age: <input type="text" name="age" /><br>
Country: <input type="text" name="country" /><br>
Other info: <input type="text" name="other" /><br>
<br>
<input type="submit" value="Search" />
</form>
</center>

</body>
</html>

and if any field is left blank, the search shows all the people in the database. :/