Php results script accesing MySQL is not displaying any info

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
apervizi
Forum Newbie
Posts: 8
Joined: Thu Jul 24, 2008 2:24 pm

Php results script accesing MySQL is not displaying any info

Post by apervizi »

My HTML search form includes a text box (the name of the company is typed) and a drop down menu (where a US state is chosen).

My MySQL database has 11 fields, but when the search is executed I only want to display 7 fields (fld_companyName, fld_address, fld_city, fld_zip, fld_warnState, fld_numEmployees and fld_termDate).

The following code also includes the php results script.

When I hit the search button, nothing is being displayed.

Can somebody please take a look at this code.

Thanks

Alim Pervizi


<form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
Search for the company: <input type="text" name="find" /> in
<select name="WarnState"> // Table Field
<option value="" selected="selected">Choose a State</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" value="search" name="search" />
</form>

<?php

//Displayed only when the form has been submitted
if ($searching =="yes")
{

echo "<h2>Results</h2><p>";

//If no search term is typed an error is displayed
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

$hostname = "localhost";
$username = "dbuser";
$password = "password";
$usertable = "dbtable";
$dbName = "databasename";

if (!mysql_connect($hostname, $username, $password))
die("Can't connect to database");

if (!mysql_select_db($dbName))
die("Can't select database");

// Perform a bit of filtering
$find = strtoupper($find); // Change characters to UPPER case
$find = strip_tags($find); // Removes out any code that may have been entered previously
$find = trim ($find); // Removes out all the whitespace

//Search based on the search term, in the field the user specified
$data = mysql_query("SELECT * FROM {$usertable}");

//Display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['fld_companyName'];
echo " ";
echo $result['fld_address'];
echo "<br>";
echo $result['fld_city'];
echo "<br>";
echo $result['fld_zip'];
echo "<br>";
echo $result['fld_warnState'];
echo "<br>";
echo $result['fld_numEmployees'];
echo "<br>";
echo $result['fld_termDate'];
echo "<br>";
echo "<br>";

}

//Counting the number or results - If there is no results, a message is displayed
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
sureshmaharana
Forum Commoner
Posts: 30
Joined: Thu Jul 03, 2008 4:20 am
Contact:

Re: Php results script accesing MySQL is not displaying any info

Post by sureshmaharana »

Add these line before if ($searching =="yes") condition.

$searching = $_POST['searching'];
$find = $_POST['find'];
apervizi
Forum Newbie
Posts: 8
Joined: Thu Jul 24, 2008 2:24 pm

Re: Php results script accesing MySQL is not displaying any info

Post by apervizi »

After inserting the code lines before if ($searching =="yes"), now I am getting the entire data from the table.

Nothing is being filtered based on what is typed (company) and what is selected from the drop down menu (state).

Thanks for helping me to sort this problem out.


Code: Select all

 
<?php
 
$searching = $_POST['searching'];
$find = $_POST['find'];
 
ini_set("display_errors","2");
ERROR_REPORTING(E_ALL); 
 
//This is only displayed if they have submitted the form 
if ($searching =="yes") 
{ 
 
echo "<h2>Results</h2><p>"; 
 
//If they did not enter a search term we give them an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 
$hostname = "localhost";
$username = "dbuser";
$password = "password";
$usertable = "dbtable";
$dbName = "databasename";
 
if (!mysql_connect($hostname, $username, $password))
die("Can't connect to database");
 
if (!mysql_select_db($dbName))
die("Can't select database");
 
// Perform a bit of filtering
$find = strtoupper($find); // Change characters to UPPER case
$find = strip_tags($find); // Removes out any code that may have been entered previously
$find = trim ($find); // Removes out all the whitespace
 
//Search based on the search term, in the field the user specified
$data = mysql_query("SELECT * FROM {$usertable}");
 
//Display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['fld_companyName'];
echo " ";
echo $result['fld_address'];
echo "<br>";
echo $result['fld_city'];
echo "<br>";
echo $result['fld_zip'];
echo "<br>";
echo $result['fld_warnState'];
echo "<br>";
echo $result['fld_numEmployees'];
echo "<br>";
echo $result['fld_termDate'];
echo "<br>";
echo "<br>";
 
}
 
//Counting the number or results - If there is no results, a message is displayed
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
 
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
 
apervizi
Forum Newbie
Posts: 8
Joined: Thu Jul 24, 2008 2:24 pm

Re: Php results script accesing MySQL is not displaying any info

Post by apervizi »

Any other suggestions on why the script doesn't work correctly?

Thanks
Post Reply