Forum Moderators: coopster

Message Too Old, No Replies

Search mysql using php dropdown menu to select appropriate column

php mysql dropdown search problem

         

alexander101

9:22 pm on Aug 14, 2007 (gmt 0)

10+ Year Member



Hi,

I am new to this website and very new to php and mysql however I am having a go at writing a really, REALLY basic contacts database and I have hit a problem.
I am trying to query the database using a text field and a drop-down menu. The drop-down menu contains a list of the columns that are in my database so the user can specify the search criteria. The text box is to contain the search string from the user.
The php page loads fine with the text box and drop-down menu displaying correctly however when I enter a search string and select a "column" and click submit, all that happens is that the page "refreshes". I was expecting a set of results to display containing relevant matches to the criteria... or at least an error! :)

I understand this is a VERY basic problem but any help would be much appreciated. Code follows:

<html>
<head>
<basefont face="Arial">
</head>
<body>

<?php

if (!isset($_POST['submit'])) {
// form not submitted
?>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Search <input type="text" name="search"><br>
<select size="1" name="dropdown">
<option value="" selected>Search By...</option>
<option value="first">First Name</option>
<option value="last">Last Name</option>
<option value="company">Company</option>
<option value="address">Street Name</option>
<option value="town">Town</option>
<option value="city">City</option>
<option value="postcode">Postcode</option>
<option value="mobile">Mobile Number</option>
<option value="phone">Phone Number</option>
<option value="email">Email Address</option>
<option value="website">Web Address</option>
</select>
<input type="submit" value="Submit">
</form>

<?php
}

else {

// Server Variables
$host = "****";
$user = "****";
$pass = "****";
$db = "****";

$search = empty($_POST['search'])? die ("ERROR: Enter Search Criteria") : mysql_escape_string($_POST['search']);
$dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from Dropdown") : mysql_escape_string($_POST['dropdown']);

// Open Connection

$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host");

//Select Database

mysql_select_db($db) or die ("Unable to connect to database");

//Create Query

$query = "SELECT * FROM contacts WHERE $dropdown='$search'" or die (mysql_error());

$result = mysql_query($query) or die (mysql_error());

$num=mysql_numrows($result);

mysql_close($connect);

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$company=mysql_result($result,$i,"company");
$address=mysql_result($result,$i,"address");
$town=mysql_result($result,$i,"town");
$city=mysql_result($result,$i,"city");
$postcode=mysql_result($result,$i,"postcode");
$mobile=mysql_result($result,$i,"mobile");
$phone=mysql_result($result,$i,"phone");
$email=mysql_result($result,$i,"email");
$website=mysql_result($result,$i,"website");

echo "<b>$first $last</b><br>Company: $company<br><br>Street Name: $address<br>Town: $town<br>City: $city<br>Postcode: $postcode<br>Mobile: $mobile<br>Phone: $phone<br>Email: $email<br>Website: $website<br><hr><br>";

$i++;

}
}
?>

</body>
</html>

dreamcatcher

6:18 am on Aug 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi alexander101,

Welcome to WebmasterWorld. :)

Try popping the following after your opening <?php tag

error_reporting(E_ALL);

Then run the script again to see if you get any errors.

dc

Habtom

6:23 am on Aug 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was expecting a set of results to display containing relevant matches to the criteria... or at least an error! :)

if (!isset($_POST['submit'])) {
// form not submitted
?>

Change submit to Submit: the name of your submit button is "Submit". Replace the above code with this one:

if (!isset($_POST['Submit'])) {
// form not submitted
?>

Habtom

alexander101

12:00 pm on Aug 15, 2007 (gmt 0)

10+ Year Member



Thank you for your help on this Dreamcatcher and Habtom however after making the changes suggested the problem is still the same. I was looking around the net for info on this problem earlier and stumbled across a tutorial on this exact topic. I copied their code and tried it on my server however the problem was the same. Is this a regular problem?

Again, thanks for the help!

Habtom

12:04 pm on Aug 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you replace the submit with the Submit?

Post the current code you have now and I will look at it.

alexander101

12:18 pm on Aug 15, 2007 (gmt 0)

10+ Year Member



Hi Habtom

Yes, I replaced 'submit' with 'Submit'. Code follows:

<html>
<head>
<basefont face="Arial">
</head>
<body>

<?php

error_reporting(E_ALL);
if (!isset($_POST['Submit'])) {
// form not submitted
?>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Search <input type="text" name="search"><br>
<select size="1" name="dropdown">
<option value="" selected>Search By...</option>
<option value="first">First Name</option>
<option value="last">Last Name</option>
<option value="company">Company</option>
<option value="address">Street Name</option>
<option value="town">Town</option>
<option value="city">City</option>
<option value="postcode">Postcode</option>
<option value="mobile">Mobile Number</option>
<option value="phone">Phone Number</option>
<option value="email">Email Address</option>
<option value="website">Web Address</option>
</select>
<input type="submit" value="Submit">
</form>

<?php
}

else {

// form submitted
// set server access variables
$host = "****";
$user = "****";
$pass = "****";
$db = "****";

$search = empty($_POST['search'])? die ("ERROR: Enter Search Criteria") : mysql_escape_string($_POST['search']);
$dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from Dropdown") : mysql_escape_string($_POST['dropdown']);

// Open Connection

$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host");

//Select Database

mysql_select_db($db) or die ("Unable to connect to database");

//Create Query

$query = "SELECT * FROM contacts WHERE $dropdown='$search'" or die (mysql_error());

$result = mysql_query($query) or die (mysql_error());

$num=mysql_numrows($result);

mysql_close($connect);

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$company=mysql_result($result,$i,"company");
$address=mysql_result($result,$i,"address");
$town=mysql_result($result,$i,"town");
$city=mysql_result($result,$i,"city");
$postcode=mysql_result($result,$i,"postcode");
$mobile=mysql_result($result,$i,"mobile");
$phone=mysql_result($result,$i,"phone");
$email=mysql_result($result,$i,"email");
$website=mysql_result($result,$i,"website");

echo "<b>$first $last</b><br>Company: $company<br><br>Street Name: $address<br>Town: $town<br>City: $city<br>Postcode: $postcode<br>Mobile: $mobile<br>Phone: $phone<br>Email: $email<br>Website: $website<br><hr><br>";

$i++;

}
}
?>

</body>
</html>

Habtom

12:28 pm on Aug 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please replace this line:
<input type="submit" value="Submit">

With the following line:
<input type="Submit" value="Submit" name="Submit">

Will be rolling up my sleeves in the mean time, if this doesn't work :)

Habtom

[edited by: Habtom at 12:28 pm (utc) on Aug. 15, 2007]

alexander101

1:31 pm on Aug 15, 2007 (gmt 0)

10+ Year Member



Aha! It works! :) Thank you so much for your help and your rapid response to my posts. Now I'm going to make the html aesthetically pleasing - Let's hope I don't mess it up!

Thanks again :)

Habtom

5:07 am on Aug 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it working.