Hello,
I’m putting together an internal company website. Right now its taking two fields firstName and lastName but I will be adding more fields (address,city,state,zip…etc). I want to create a dynamic select statement that will look at what fields the user has entered and use those in the select statement. I did some research and found two query generators/builders (http://www.phpkode.com/source/s/mysql-query-generator/mysql-query-generator/mquery.class.php and http://code.google.com/p/mysql-query-builder/source/browse/trunk/BasicQuery.php?spec=svn23&r=23). I tried applying them to my code with no success. Is this the approach I should be taking or something else ? I have posted my php file below. Thanks in advance.
[php]
<?php $dbhost = "localhost"; $dbuser = "root"; $dbpass = "test"; $dbname = "test"; //Connect to MySQL Server mysql_connect($dbhost, $dbuser, $dbpass); //Select Database mysql_select_db($dbname) or die(mysql_error()); // Retrieve data from Query String $firstName = $_GET['firstName']; $lastName = $_GET['lastName']; // Escape User Input to help prevent SQL Injection $firstName = mysql_real_escape_string($firstName); $lastName = mysql_real_escape_string($lastName); //build query $query = "SELECT * FROM `swipemaster` WHERE `FirstName` = '$firstName' AND `LastName` = '$lastName'"; //Execute query $qry_result = mysql_query($query) or die(mysql_error()); //Build Result String $display_string = "First Name | "; $display_string .= "Middle Name | "; $display_string .= "Last Name | "; $display_string .= "Suffix | "; $display_string .= "Address | "; $display_string .= "City | "; $display_string .= "State | "; $display_string .= "Zip Code | "; $display_string .= "License Number | "; $display_string .= "Expiration | "; $display_string .= "Birthdate | "; $display_string .= "Gender | "; $display_string .= "Status | "; $display_string .= "Date Scanned | "; $display_string .= "Time Scanned | "; $display_string .= "Banned | "; $display_string .= "DJ | "; $display_string .= "Opt | "; $display_string .= "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$row[firstName] | "; $display_string .= "$row[middleName] | "; $display_string .= "$row[lastName] | "; $display_string .= "$row[suffix] | "; $display_string .= "$row[address] | "; $display_string .= "$row[city] | "; $display_string .= "$row[state] | "; $display_string .= "$row[zipCode] | "; $display_string .= "$row[licenseNumber] | "; $display_string .= "$row[expiration] | "; $display_string .= "$row[birthdate] | "; $display_string .= "$row[gender] | "; $display_string .= "$row[status] | "; $display_string .= "$row[dateScanned] | "; $display_string .= "$row[timeScanned] | "; $display_string .= "$row[banned] | "; $display_string .= "$row[dj] | "; $display_string .= "$row[opt] | "; $display_string .= "
[/php]
-Chris