Created
November 28, 2011 09:08
-
-
Save numberwhun/1399706 to your computer and use it in GitHub Desktop.
Process Data Request From Form - With Database Connectivity and Searching: This is smart enough that it handles forms with multiple options to narrow search.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$host = 'localhost'; /* Set to location of mysql server */ | |
$dbname = 'database_name'; | |
$user = 'user_name'; | |
$pwd = 'password'; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Required file for processing */ | |
require("./config.php.inc"); | |
/* This is to take the populated options in the POST array and put them into an array, minus the null options */ | |
$opts_array = array_filter($_POST); | |
/* Get a count of the number of options in the opts_array so we know what we are dealing with */ | |
$options_count = count($opts_array); | |
/* Check the data submitted. If options_count is greater the 1 and the opts array includes | |
emaildomain, then exit with an error */ | |
if($options_count >= "2"){ | |
if(array_key_exists('contactemailid', $opts_array)){ | |
exit("You have submitted multiple fields from the search form, and one of them appears to be <b>emaildomain</b>. This option must be entered by itself with no other options. Please use the BACK button of your browser to go back and correct your search criteria."); | |
} | |
} | |
/* Get the keys into an array where they are the values */ | |
$opts_keys = array_keys($opts_array); | |
/* Create a new connection to the database */ | |
try{ | |
$dbconn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pwd); | |
$dbconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
} | |
catch(PDOException $e){ | |
showHeader('Error'); | |
showError("Sorry, an error has occurred. \n" . $e->getMessage()); | |
} | |
/* print_r($opts_array); | |
echo("<br />"); */ | |
/* Construct the SQL statement based on the number of options supplied */ | |
if($options_count == "1"){ | |
if(array_key_exists('contactemailid', $opts_array)){ | |
$sql = "SELECT id, partnername from client_info where $opts_keys[0] LIKE '%@" . $opts_array[$opts_keys[0]] . "'"; | |
} | |
else{ | |
$sql = "SELECT id, partnername from client_info where $opts_keys[0] LIKE '%" . $opts_array[$opts_keys[0]] . "%'"; | |
} | |
} | |
elseif($options_count == "2"){ | |
$sql = "SELECT id, partnername from client_info where $opts_keys[0]='" . $opts_array[$opts_keys[0]] . "' and $opts_keys[1]='" . $opts_array[$opts_keys[1]] . "'"; | |
} | |
elseif($options_count == "3"){ | |
$sql = "SELECT id, partnername from client_info where $opts_keys[0]='" . $opts_array[$opts_keys[0]] . "' and $opts_keys[1]='" . $opts_array[$opts_keys[1]] . "' and $opts_keys[2]='" . $opts_array[$opts_keys[2]] . "'"; | |
} | |
elseif($options_count == "4"){ | |
$sql = "SELECT id, partnername from client_info where $opts_keys[0]='" . $opts_array[$opts_keys[0]] . "' and $opts_keys[1]='" . $opts_array[$opts_keys[1]] . "' and $opts_keys[2]='" . $opts_array[$opts_keys[2]] . "' and $opts_keys[3]='" . $opts_array[$opts_keys[3]] . "'"; | |
} | |
elseif($options_count == "5"){ | |
$sql = "SELECT id, partnername from client_info where $opts_keys[0]='" . $opts_array[$opts_keys[0]] . "' and $opts_keys[1]='" . $opts_array[$opts_keys[1]] . "' and $opts_keys[2]='" . $opts_array[$opts_keys[2]] . "' and $opts_keys[3]='" . $opts_array[$opts_keys[3]] . "' and $opts_keys[4]='" . $opts_array[$opts_keys[4]] . "'"; | |
} | |
elseif($options_count == "6"){ | |
$sql = "SELECT id, partnername from client_info where $opts_keys[0]='" . $opts_array[$opts_keys[0]] . "' and $opts_keys[1]='" . $opts_array[$opts_keys[1]] . "' and $opts_keys[2]='" . $opts_array[$opts_keys[2]] . "' and $opts_keys[3]='" . $opts_array[$opts_keys[3]] . "' and $opts_keys[4]='" . $opts_array[$opts_keys[4]] . "' and $opts_keys[5]='" . $opts_array[$opts_keys[5]] . "'"; | |
} | |
elseif($options_count == "7"){ | |
echo "You have entered 7 options on the search screen. This would include the email domain, which MUST be entered by itself."; | |
echo "<br />"; | |
echo "Please use the BACK button and re-enter you search criteria."; | |
} | |
/* Query the db and get data */ | |
$q = $dbconn->query($sql); | |
/* Commented out -> Only used for QA purposes to ensure code works */ | |
/* while($r = fetch($q)) | |
while($r = $q->fetchall(PDO::FETCH_ASSOC)){ | |
print_r($r); | |
} | |
*/ | |
/* Function to test if a number is odd or even */ | |
function is_odd($number) { | |
return $number & 1; // 0 = even, 1 = odd | |
} | |
/* Display the results of the search to the user */ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment