Jump to content

Simple Search Form


Smee

Recommended Posts

Hi,

 

Im having trouble with a simple search form. As you can tell by this im new and any changes would be more than welcome. All i want to do is be able to search the database by post code and team supported. This would return results in the form of, first name, last name, email, post code and team supported.

 

Thanks in advance!

 


<?php 

// This script allows us to search our database

require_once ('./includes/config.inc.php');

$page_title = 'Search';

include ('./includes/header.html');

if ($post_code) // perform search only if a string was entered.
if ($team_supported) 

{
include_once ('..mysql_connect.php');

$query = "SELECT * from users WHERE post_code='$post_code' AND team_supported='$team_supported'";

$result = mysql_db_query("post_code", "team_supported", $query);

if ($result)
{
echo "Here are the results:<br><br>";
echo "<table width=90% align=center border=1><tr>
<td align=center bgcolor=#00FFFF>First Name</td>
<td align=center bgcolor=#00FFFF>Last Name</td>
<td align=center bgcolor=#00FFFF>E-Mail</td>
<td align=center bgcolor=#00FFFF>Team Supported</td>
<td align=center bgcolor=#00FFFF>Post Code</td>
</tr>";

while ($r = mysql_fetch_array($result)) { // Begin while
$fn = $r["first_name"];
$ln = $r["last_name"];
$em = $r["email"];
$ts = $r["team_supported"];
$pc = $r["post_code"];

echo "
<tr>
<td>$fn</td>
<td>$last_name</td>
<td>$email</td>
<td>$team_supported</td>
<td>$post_code</td>
</tr>";

} // end while

echo "</table>";

} else { 

echo "problems...."; }

} else {

echo "Search string is empty. <br> Go back and type a string to search";
}

?> 

<h1> Search </h1>

<form action="search.php" method="post" />

<p class="post_code"><label> Post Code: </label>
<input type="text" name='post_code' maxlength="40" />

<p class="team_supported"><label> Post Code: </label>
<input type="text" name='team_supported' maxlength="40" />
            
<p class="submit">
<input type="submit" name="submit" value="Reset Password" />
            
</form>

<?php
include ('./includes/footer.html');
?>

Link to comment
Share on other sites

Thanks for the reply..

 

Ok i have makes several adjustments and have no errors in the script but now when you click 'search' it doesnt show any results when it should do. Here is the revised script:

 

<?php 

// This script allows us to search our database

require_once ('./includes/config.inc.php');

$page_title = 'Search';

include ('./includes/header.html');

if (isset($_POST['submit'])) {

require_once ('../mysql_connect.php');

if (empty($_POST['post_code'])) { // Validate Email Address
echo '<p><font color="red" size="+1">Please enter your post code.</font></p>';

if (empty($_POST['team_supported'])) { // Validate Email Address
echo '<p><font color="red" size="+1">Please enter the team you support.</font></p>';

}

} else {

$query ="SELECT * FROM users WHERE post_code='". escape_data($_POST['post_code']) . "' AND team_supported='". escape_data($_POST['team_supported']) . "'";
$result = mysql_query ($query);

if (mysql_num_rows($result) == 1) {

echo "Here are the results:<br><br>";
echo "<table width=90% align=center border=1><tr>
<td align=center bgcolor=#00FFFF>First Name</td>
<td align=center bgcolor=#00FFFF>Last Name</td>
<td align=center bgcolor=#00FFFF>E-Mail</td>
<td align=center bgcolor=#00FFFF>Team Supported</td>
<td align=center bgcolor=#00FFFF>Post Code</td>
</tr>";

while ($r = mysql_fetch_array($result)) // Begin while
$fn = $r["first_name"];
$ln = $r["last_name"];
$em = $r["email"];
$ts = $r["team_supported"];
$pc = $r["post_code"];

echo "
<tr>
<td>$fn</td>
<td>$last_name</td>
<td>$email</td>
<td>$team_supported</td>
<td>$post_code</td>
</tr>";

} // end while

}

}


?> 

<h1> Search </h1>

<form action="search.php" method="post" />

<p class="post_code"><label> Post Code: </label>
<input type="text" name="post_code" maxlength="40" value="<?php if (isset($_POST['post_code'])) echo $_POST['post_code']; ?>"/>

<p class="team_supported"><label> Team Supported: </label>
<input type="text" name="team_supported" maxlength="40" value="<?php if (isset($_POST['team_supported'])) echo $_POST['team_supported']; ?>" />
            
<p class="submit">
<input type="submit" name="submit" value="Search" />
            
</form>

<?php
include ('./includes/footer.html');
?>

 

 

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.