fross Posted August 28, 2011 Share Posted August 28, 2011 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. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted August 28, 2011 Share Posted August 28, 2011 this line: While (($Row = mysql_fetch_row($queryresult)) !== FALSE) { only needs to be this: While ($Row = mysql_fetch_row($queryresult)) { Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted August 28, 2011 Share Posted August 28, 2011 also you've added an extra equals sign here... $State == $_POST['state']; should be: $State = $_POST['state']; Quote Link to comment Share on other sites More sharing options...
fross Posted August 28, 2011 Author Share Posted August 28, 2011 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); } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 28, 2011 Share Posted August 28, 2011 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(); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.