Jump to content

Searching Related tables


StressCase

Recommended Posts

Hello everyone I need help creating a search form to query related tables

I have a table called customer and a table called customercontact

when the page loads if there is no search criteria I'm displaying all the customers with their respective contact

my problem is when they enter a search criteria it reutrns all the contacts in the database for each result

i.e. 50 contacts in the db and search returned to possible matches then the result is 100 and they all get named with criteria.

 

I'm stuck on this :confused: any help would be greatly appreciated.

 

$search = "-1";
if (!isset($_GET['searchtext'])) {

$result = mysql_query("SELECT 
				  customer.CustomerID, customer.CompanyName, customer.Address, customercontact.FirstName, customercontact.LastName 
				  FROM
				  customer, customercontact 
				  WHERE
				  customercontact.CompanyID = customer.CustomerID") or die (mysql_error());

$row = mysql_fetch_assoc($result);
}
else {
 $search = $_GET['searchtext'];

 $result = mysql_query("SELECT
					   customer.CustomerID, customer.CompanyName, customer.Address, customercontact.FirstName, customercontact.LastName
					   FROM
					   customer, customercontact
					   WHERE 
					   CompanyName like '%" . $search . "%' OR MainTel like '%" . $search . "%' AND customer.CustomerID = customercontact.CompanyID
					   ORDER BY 
					   CompanyName ASC") or die (mysql_error());

 $row = mysql_fetch_assoc($result);
}

Link to comment
https://forums.phpfreaks.com/topic/181680-searching-related-tables/
Share on other sites

Update.. I'm still stuck on this but I found if I remove one search criteria it works perfectly

Hopefully someone knows hows I can add more search criteria.

Thanks in advance.

 

so now the code looks like this...

<?php
$search = "-1";
if (!isset($_GET['searchtext'])) {
// If there is no search criteria Get all records
$result = mysql_query("SELECT 
				  customer.CustomerID, customer.CompanyName, customer.Address, customercontact.FirstName, customercontact.LastName 
				  FROM
				  customer, customercontact 
				  WHERE
				  customercontact.CompanyID = customer.CustomerID") or die (mysql_error());

$row = mysql_fetch_assoc($result);
}
else {
//assign search
 $search = $_GET['searchtext'];
 //query db based on the search criteria
 $result = mysql_query("SELECT
					   customer.CustomerID, customer.CompanyName, customer.Address, customercontact.FirstName, customercontact.LastName
					   FROM
					   customer, customercontact
					   WHERE 
					   customer.CompanyName like '%" . $search . "%' AND customer.CustomerID = customercontact.CompanyID
					   ORDER BY 
					   CompanyName ASC") or die (mysql_error());
 //return the search results
 $row = mysql_fetch_assoc($result);
}
?>

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.