I have a database that has customer first and last names as well as phone numbers, the problem is when anyone just types in the area code it will show all customers with that same area code.
how do I write the code that tells the user they HAVE to put in all 10 digits of their phone number or else the SQL will die?
SUB-QUESTION: The database phone numbers are all hyphenated like this - 777-555-1234. What code can I use to trim or boolean those hyphens out so that it wouldn’t matter if they put hyphens in or not.
Here is my code for those interested in helping out.
<?php
/**************************************************************************************
* Main Search Page - search.php
* Author: Your Name <[email protected]>
* This file searches the database
**************************************************************************************/
//Get variables from config.php to connect to mysql server
require 'config.php';
// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
//select the database
mysql_select_db($dbname) or die('Cannot select database');
//search variable = data in search box or url
if(isset($_GET['search']))
{
$search = $_GET['search'];
}
//trim whitespace from variable
$search = trim($search);
$search = preg_replace('/s+/', ' ', $search);
//seperate multiple keywords into array space delimited
$keywords = explode(" ", $search);
//Clean empty arrays so they don't get every row as result
$keywords = array_diff($keywords, array(""));
//Set the MySQL query
if ($search == NULL or $search == '%'){
} else {
for ($i=0; $i<count($keywords); $i++) {
$query = "SELECT * FROM Customer " .
"WHERE Phone LIKE '%".$keywords[$i]."%'".
" ORDER BY LastName";
}
//Store the results in a variable or die if query fails
$result = mysql_query($query) or die(mysql_error());
}
if ($search == NULL or $search == '%'){
} else {
//Count the rows retrived
$count = mysql_num_rows($result);
}
echo "<html>";
echo "<head>";
echo "<title>Your Title Here</title>";
echo "<link rel="stylesheet" type="text/css" href="style.css" />";
echo "</head>";
echo "<body onLoad="self.focus();document.searchform.search.focus()">";
echo "<center>";
echo "<br /><form name="searchform" method="GET" action="search.php">";
echo "<input type="text" name="search" size="20" TABINDEX="1" />";
echo " <input type="submit" value="Search" />";
echo "</form>";
//If search variable is null do nothing, else print it.
if ($search == NULL) {
} else {
echo "You searched for <b><FONT COLOR="blue">";
foreach($keywords as $value) {
print "$value ";
}
echo "</font></b>";
}
echo "<p> </p><br />";
echo "</center>";
//If users doesn't enter anything into search box tell them to.
if ($search == NULL){
echo "<center><b><FONT COLOR="red">Please enter a search parameter to continue.</font></b><br /></center>";
} elseif ($search == '%'){
echo "<center><b><FONT COLOR="red">Please enter a search parameter to continue.</font></b><br /></center>";
//If no results are returned print it
} elseif ($count <= 0){
echo "<center><b><FONT COLOR="red">Your query returned no results from the database.</font></b><br /></center>";
//ELSE print the data in a table
} else {
//Table header
echo "<center><table width="600px" id="search" bgcolor="#AAAAAA">";
echo "<tr>";
echo "<td style="table-layout:fixed;width:300px"><b>First Name</b></td>";
echo "<td style="table-layout:fixed;width:300px"><b>Last Name</b></td>";
echo "<td style="table-layout:fixed;width:300px"><b>Points</b></td>";
echo "<tr>";
echo "</table></center>";
//Colors for alternation of row color on results table
$color1 = "ffffff";
$color2 = "666666";
//While there are rows, print it.
while($row = mysql_fetch_array($result))
{
//Row color alternates for each row
$row_color = ($row_count % 2) ? $color1 : $color2;
//table background color = row_color variable
echo "<center><table width="600px" bgcolor=".$row_color.">";
echo "<tr>";
echo "<td style="table-layout:fixed;width:300px">".$row['CustName']."</td>";
echo "<td style="table-layout:fixed;width:300px">".$row['LastName']."</td>";
echo "<td style="table-layout:fixed;width:300px">".$row['CustLoyaltyPoints']."</td>";
echo "</tr>";
echo "</table></center>";
$row_count++;
//end while
}
//end if
}
echo "</body>";
echo "</html>";
if ($search == NULL or $search == '%') {
} else {
//clear memory
mysql_free_result($result);
}
?>
Thank you!