Jump to content

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);

            


        ?> 

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.