Solved

Mutl-Field Search PHP

Posted on 2013-01-08
2
251 Views
Last Modified: 2013-01-09
I have a mult-field search and I'm trying to figure out how to post it and only search for fields that were selected.

<?php

$programid = "2036";

require_once($_SERVER['DOCUMENT_ROOT']."/includes/verifyaccess.php");
require_once($_SERVER['DOCUMENT_ROOT']."/includes/mainfunctions.php");
require_once($_SERVER['DOCUMENT_ROOT']."/includes/expFunctions.php");
require_once($_SERVER['DOCUMENT_ROOT']."/includes/header.php");

//Make sure search for was submitted properly

if(isset($_POST['submit'])) 
	{
    //define the list of fields
    $fields = array('kdaccount', 'name', 'phone', 'town', 'state', 'sic', 'referral', 'kstatus', 'nlrep');
	
	//put conditionals in an array
    $conditions = array();
	
	foreach($fields as $field)
		{
        // if the field is set and not empty
        if(isset($_POST[$field]) && $_POST['field'] != '') 
			{
			// create a new condition while escaping the value inputed by the user (SQL Injection)
			$conditions[] = "`$field` LIKE '%" . mysql_real_escape_string($_POST[$field]) . "%'";
			}
		}

	// builds the query
	$query = "SELECT name, kdaccount FROM dw_names ";
		
	// if there are conditions defined
	if(count($conditions) > 0) 
		{
		// append the conditions
		$query .= "WHERE " . implode (' AND ', $conditions);
		}
	
	$result = mysql_query($query);
	echo $query . '<br />';
	}]

Open in new window

<?php

$programid = "2036";

require_once($_SERVER['DOCUMENT_ROOT']."/includes/verifyaccess.php");
require_once($_SERVER['DOCUMENT_ROOT']."/includes/mainfunctions.php");
require_once($_SERVER['DOCUMENT_ROOT']."/includes/header.php");

?>

<span class="sectitle">Contact Management Search Form</span><br /><br />

<form action="searchresults.php" method="POST">
	<table width="100%" cellspacing="0" cellpadding="0" border="0">
		<tr>
			<td class="td_leftCol3" width="175px">Account Code:</td>
			<td class="td_rightCol"><input type="text" name="kdaccount" class="formFields"></td>
		</tr>
		<tr>
			<td class="td_leftCol3" width="175px">Company Name:</td>
			<td class="td_rightCol"><input type="text" name="name" class="formFields"></td>
		</tr>	
		<tr>
			<td class="td_leftCol3" width="175px">Company Phone:</td>
			<td class="td_rightCol"><input type="text" name="phone" class="formFields"></td>
		</tr>
		<tr>
			<td class="td_leftCol3" width="175px">Location:</td>
			<td class="td_rightCol"><input type="text" name="town" class="formFields"></td>
		</tr>
		<tr>
			<td class="td_leftCol3" width="175px">Contact Name:</td>
			<td class="td_rightCol"><input type="text" name="contactname" class="formFields"></td>
		</tr>
		<tr>
			<td class="td_leftCol3" width="175px">Contact Phone:</td>
			<td class="td_rightCol"><input type="text" name="contactphone" class="formFields"></td>
		</tr>
		<tr>
			<td class="td_leftCol3" width="175px">State:</td>
			<td class="td_rightCol">
				<select name="state" class="formFields">
					<option value="">Select a State...</option>
					<?php 
					$sql = mysql_query("SELECT stateid, state FROM dw_states ORDER BY state");
					
					if(mysql_num_rows($sql) > 0)
						{
						while($stateData = mysql_fetch_array($sql))
							{
							echo '<option value = "'.$stateData['stateid'].'">'.$stateData['state'].'</option>';
							}
						}
					?>
			</td>
		</tr>
		<?php if($groupid == '901' || $groupid == '910' || $groupid == '929' || $groupid == '999')
			{
			echo '
			<tr>
				<td class="td_leftCol3" width="175px">SIC Code:</td>
				<td class="td_rightCol">
					<select name="sic" class="formFields">
						<option value="">Select a SIC Code...</option>';
						
						$sql2 = mysql_query("SELECT sic4, sic4desc FROM dw_names_sic GROUP BY sic4 ORDER BY sic4 ASC");
						
						if(mysql_num_rows($sql2) > 0)
							{
							while($sicData = mysql_fetch_array($sql2))
								{
								echo '<option value = "'.$sicData['sic4'].'">'.$sicData['sic4desc'].'</option>';
								}
							}
				echo '
					</select>
				</td>
			</tr>';
			}
		?>
		<tr>
			<td class="td_leftCol3" width="175px">Referred By:</td>
			<td class="td_rightCol">
				<select name="referral" class="formFields">
					<option value="">Select Referral...</option>
					<?php
					$sql3 = mysql_query("SELECT refid, referredby FROM pr_referral ORDER BY referredby");
					
					if(mysql_num_rows($sql2) > 0)
						{
						while($referralData = mysql_fetch_array($sql3))
							{
							echo '<option value = "'.$referralData['refid'].'">'.$referralData['referredby'].'</option>';
							}
						}
					echo '
				</select>
			</td>	
		</tr>';
		?>
		
		<tr>
			<td class="td_leftCol3" width="175px">Result/Prospect/All:</td>
			<td class="td_rightCol">
				<select name="comptype" class="formFields">
					<option value="1">All</option>
					<option value="2">Prospect</option>
					<option value="3">Result</option>
				</select>
			</td>
		</tr>
		<tr>
			<td class="td_leftCol3" width="175px">Status:</td>
			<td class="td_rightCol">
				<select name="kstatus" class="formFields">
					<option value="1">All</option>
					<option value='2'>Active</option>
					<option value='3'>Deactivated</option>
					<option value='4'>On Hold</option>			
				</select>
			</td>
		</tr>
		<?php if($groupid == '901' || $groupid == '910' || $groupid == '929' || $groupid == '999')
			{
			echo '
			<tr>
				<td class="td_leftCol3" width="175px">Sales Rep:</td>
				<td class="td_rightCol">
					<select name="nlrep" class="formFields">
						<option value="">Select a Rep...</option>';
						
						$sql4 = mysql_query("SELECT krep, ckname FROM pr_resp WHERE hidden = '0' and itresp = '0' and (responsibility = '2' OR responsibility = '4') ORDER BY ckname");
						
						if(mysql_num_rows($sql4) > 0)
							{
							while($repData = mysql_fetch_array($sql4))
								{
								echo '<option value="'.$repData['krep'].'">'.$repData['ckname'].'</option>';
								}
							}
						echo '
					</select>
				</td>
			</tr>';
			}
		?>
		<tr>
			<td colspan="2">&nbsp;</td>
		</tr>
		<tr>
			<td colspan="2" align="center"><input type="submit" name="submit" value = "Search!"></td>
		</tr>
	</table>
</form>

Open in new window

0
Comment
Question by:t3chguy
2 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 38758158
Your line 23 in the first file should be:
        if(isset($_POST[$field]) && !empty($_POST[$field]))
0
 
LVL 1

Author Closing Comment

by:t3chguy
ID: 38758685
That did it.  What a dumb oversight!  Thanks!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question