Jump to content

[SOLVED] filtering results with php query


doood

Recommended Posts

This code works, but I don't know how to filter the results to where only one person is displayed..it displays all records with similar address or state or city, I would like to know how to make it display only one person when multiple search fields are entered.

 

I'm using PHP5, and I have searched the forums but I'm not sure exactly if I found what I'm looking for...I don't know much about php or mysql.

 

Any help would be appreciated. Thanks

<?php

        include 'connect.php';

        mysql_select_db("pserver_ITSE2302")
        or die(mysql_error());

	$name = $_POST['name'];
	$address = $_POST['address'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$zip = $_POST['zip'];
   
        if($name != "") 
        $query = "SELECT * FROM lab5 WHERE name = '$name'";
        
        if($address != "")        
        $query = "SELECT * FROM lab5 WHERE address = '$address'";
       
        if($city != "")
        $query = "SELECT * FROM lab5 WHERE city = '$city'";
        
        if($state != "")
        $query = "SELECT * FROM lab5 WHERE state = '$state'";

        if($zip != "")
        $query = "SELECT * FROM lab5 WHERE zip = '$zip'";



        $result = mysql_query($query) 
        or die (mysql_error());


        if($result)
        {
        while($row = mysql_fetch_array($result))
            {
              $name = $row['name'];
              $address = $row['address'];
              $city = $row['city'];
              $state = $row['state'];
              $zip = $row['zip'];
          
              echo "<table>";
              echo "<tr>";
              echo "<td>";
              echo $name;
              echo "</td>";
              echo "<td>";
              echo $address;
              echo "</td>";
		  echo "<td>";
		  echo $city;
		  echo "</td>";
		  echo "<td>";
		  echo $state;
		  echo "</td>";
		  echo "<td>";
		  echo $zip;
		  echo "</td>";
              echo "</tr>";
              echo "</table>";
            }
         }
       else 
       echo "Could not retrieve records: %s\n", mysql_error($connect);

            


        ?> 

Link to comment
https://forums.phpfreaks.com/topic/44076-solved-filtering-results-with-php-query/
Share on other sites

try this

 

<?php

        include 'connect.php';

        mysql_select_db("pserver_ITSE2302")
        or die(mysql_error());

	$name = $_POST['name'];
	$address = $_POST['address'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$zip = $_POST['zip'];

	$query = "";

        if($name != "") 
        $query .= "name = '$name' AND ";
        
        if($address != "")        
        $query .= "address = '$address' AND ";
       
        if($city != "")
        $query .= "city = '$city' AND ";
        
        if($state != "")
        $query .= "state = '$state' AND ";

        if($zip != "")
        $query .= "zip = '$zip' AND ";

	$query = trim($query,"AND ");

	$FullQuery = "SELECT * FROM lab5 WHERE $query";

        $result = mysql_query($FullQuery) 
        or die (mysql_error());


        if($result)
        {
        while($row = mysql_fetch_array($result))
            {
              $name = $row['name'];
              $address = $row['address'];
              $city = $row['city'];
              $state = $row['state'];
              $zip = $row['zip'];
          
              echo "<table>";
              echo "<tr>";
              echo "<td>";
              echo $name;
              echo "</td>";
              echo "<td>";
              echo $address;
              echo "</td>";
		  echo "<td>";
		  echo $city;
		  echo "</td>";
		  echo "<td>";
		  echo $state;
		  echo "</td>";
		  echo "<td>";
		  echo $zip;
		  echo "</td>";
              echo "</tr>";
              echo "</table>";
            }
         }
       else 
       echo "Could not retrieve records: %s\n", mysql_error($connect);

            


        ?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.