Jump to content

php search returns all table data


brad12345

Recommended Posts

hi all

when i don't specify a search criteria and leave all fields blank in my form it returns the entire table and all of its contents

How do i make it return nothing instead?

 

heres my code 

//Owner POST variables///
$phoneNumber = $_POST['phoneNumber'];
$address = $_POST['address'];
$ownerName = $_POST['ownerName'];


//Patient POST vaiables//
$petName = $_POST['petName'];
$petSpecies = $_POST['petSpecies'];
$petBreed = $_POST['petBreed'];
$petAge = $_POST['petAge'];
$petWeight = $_POST['petWeight'];

$search = $_POST['search'];


///If the Search form has been filled out execute this code///
if (isset($_POST['search']))
{
	function BuildQuery($colname, $colvalue, $resetCounter = false ) {
	  static $counter = 0; //This keeps track of how may times the function BuildQuery has been called

		if ($resetCounter)
		 $counter = 0;

		if  ( (!isset($colvalue)) || (empty($colvalue)) )
			return '';//////////If the value that is passed to the function is empty it will return nothing


		$counter++; ////////Counter auto incrementing each pass

		if ($counter <= 1)
			return "where $colname = '$colvalue' "; //////////The first time the function is called it will default to "where"

		return "and $colname = '$colvalue' ";	///////////The second time the function is called it will be "and"
	}

	//////////////////////////////////////////////////////////
	/////// Building the first query dynamically
	$selectString = "select * from tblPatientBK ";
	$selectString .= BuildQuery("petName",$petName);
	$selectString .= BuildQuery("petSpecies",$petSpecies);
	$selectString .= BuildQuery("petBreed",$petBreed);
	$selectString .= BuildQuery("petAge",$petAge);
	$selectString .= BuildQuery("petWeight",$petWeight);

	$query1 = mysql_query($selectString);	////storing the first query into $query1

/////////////////////////////////////////////////////////////////////	
//Create first row with the column names for the patient table	
echo ("<table valign='top' border=1>");
echo ("<tr>");
echo ("<td valign='top' width=20><b>Patient ID</b></td>");
echo ("<td valign='top' width=200><b>Patient Name</b></td>");
echo ("<td valign='top' width=230><b>Pet Species</b></td>");
echo ("<td valign='top' width=200><b>petBreed</b></td>");
echo ("<td valign='top' width=200><b>Pet Age</b></td>");
echo ("<td valign='top' width=200><b>Pet weight</b></td>");
echo ("<td valign='top' width=200><b>Owner ID</b></td>");
echo ("</tr>");	

//////////////////////////////////////////////////////////////
////Out putting the data gathered from $query1 below the Patient tables relevant columns
    while ($row = mysql_fetch_row($query1))
		{

			echo("<tr>");		
			foreach($row as $value)
				echo("<td width=100>$value</td>");  
				echo("</tr>");
		}
           print (mysql_error());
		echo("</table>");	





	//////////////////////////////////////////////////////////
	/////// Building the second query 
	$selectString = "select * from tblOwnerBK ";
	$selectString .= BuildQuery("name",$ownerName,true);
	$selectString .= BuildQuery("address",$address);
	$selectString .= BuildQuery("phoneNumber",$phoneNumber);
        echo '<br>';

	$query2 = mysql_query($selectString); //storing the second query into $query2


/////////////////////////////////////////////////////////////////////	
//Create second row with the column names for the owner table below the Owner tables relevant columns
echo ("<table valign='top' border=1>");
echo ("<tr>");
echo ("<td valign='top' width=20><b>Owner ID</b></td>");
echo ("<td valign='top' width=20><b>Owner title</b></td>");
echo ("<td valign='top' width=200><b>Owner Name</b></td>");
echo ("<td valign='top' width=230><b>Address</b></td>");
echo ("<td valign='top' width=200><b>Phone Number</b></td>");
echo ("</tr>");	
//////////////////////////////////////////////////////////////
////Out putting the data gathered from $query2
    while ($row = mysql_fetch_row($query2))
		{

			echo("<tr>");		
			foreach($row as $value)
				echo("<td width=100>$value</td>");  
				echo("</tr>");
		}
           print (mysql_error());
		echo("</table>");	

 

thanks for any help

Link to comment
https://forums.phpfreaks.com/topic/100590-php-search-returns-all-table-data/
Share on other sites

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.