Jump to content

Searching mySQL table by variable


fross

Recommended Posts

Hello, I am working on a project and I cant seem to get this to work. I have a database (Kingsbury) that stores a contact list. I am trying to make an page were a user can search the database by state. The code from the HTML site is:

 

<form action = "sqlllist.php" method="post"><br />
Please enter the state you would like to search:     <input type="text" name="state"  /><br />
<p><input type="submit" value="Enter" /></p>
</form>

 

And then from the .php file

 

<?php

$DBConnect = mysql_connect("*****", "*****", "********");


if ($DBConnect===FALSE) 
echo "<p>Connection Failed!.</p>\n";


else {

$State == $_POST['state'];
$Result = mysql_select_db("Kingsbury", $DBConnect);
$queryresult = @mysql_query("SELECT * FROM contacts  WHERE State = $State'", $DBConnect);
echo "<table width='100%' border='1'>\n";
echo "<tr><th>User ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
<th>State</th>
<th>City</th>
<th>Zip Code</th>
<th>Area Code</th>
<th>Phone Number</th>\n";

While (($Row = mysql_fetch_row($queryresult)) !== FALSE) {
echo "<tr><td>{$Row[0]}</td>";
echo "<td>{$Row[1]}</td>";
echo "<td>{$Row[2]}</td>";
echo "<td>{$Row[3]}</td>";
echo "<td>{$Row[4]}</td>";
echo "<td>{$Row[5]}</td>";
echo "<td>{$Row[6]}</td>";
echo "<td>{$Row[7]}</td>";
echo "<td>{$Row[8]}</td>";

}
echo "</table>\n";
mysql_free_result($queryresult);
mysql_close($DBConnect);
}

?>

 

This, to me seems like it should be working, but it outputs the entire database.

Link to comment
Share on other sites

Thanks for the response, I made the changes to the code but its still not working. Now my table comes up, but there is no entries. At current, my code is:

 

<?php

$DBConnect = mysql_connect("localhost", "user1", "password1");

echo "<p> MySQL Client Version: " . mysql_get_client_info() . "</p>\n";

if ($DBConnect===FALSE) 
echo "<p>Connection Failed!.</p>\n";


else 
{

$State = $_POST['state'];
$Result = mysql_select_db("Kingsbury", $DBConnect);
$queryresult = @mysql_query("SELECT * FROM contacts WHERE State = $State", $DBConnect);
echo "<table width='100%' border='1'>\n";
echo "<tr><th>User ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
<th>State</th>
<th>City</th>
<th>Zip Code</th>
<th>Area Code</th>
<th>Phone Number</th>\n";

While ($Row = mysql_fetch_row($queryresult)) 
{
echo "<tr><td>{$Row[0]}</td>";
echo "<td>{$Row[1]}</td>";
echo "<td>{$Row[2]}</td>";
echo "<td>{$Row[3]}</td>";
echo "<td>{$Row[4]}</td>";
echo "<td>{$Row[5]}</td>";
echo "<td>{$Row[6]}</td>";
echo "<td>{$Row[7]}</td>";
echo "<td>{$Row[8]}</td>";

}
echo "</table>\n";
mysql_free_result($queryresult);
mysql_close($DBConnect);
}

?>

Link to comment
Share on other sites

Unless the 'State' field in the database is a numeric field, you need to enclose the search value in single quote marks. Your query is likely failing, add some error handling to your code so you can see errors when they occur.

 

	
mysql_select_db("Kingsbury", $DBConnect); //Don't nee to add to a variable, since you don't use it
$query = "SELECT * FROM contacts WHERE State = '$State'";
$queryresult = @mysql_query($query, $DBConnect);
if(!$queryresult)
{
    echo "Query: {$query}<br>Error: " . mysql_error();
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.