Jump to content

Saerch Form


marcs910

Recommended Posts

Im trying to begin with a simple search form and build up to an advanced search form.

 

When I run this code I get the same record back everytime no matter what I put in.

 

<?php # Script 7.4 - view_homes.php

 

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

 

echo '<h1 id="mainhead">Homes</h1>';

 

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

 

require_once ('./includes/mysql_connect.php'); // Connect to the db.

 

$st = escape_data($_POST['stories']);

 

$query = "SELECT * FROM homes WHERE stories = '$st' ORDER BY zip ASC ";

$result=mysql_query($query);

 

if ($result) {

 

echo '<table align="center" cellspacing="0" cellpadding="5">

<tr><td align="left"><b>Address</b></td>

<td align="left"><b>Location</b></td>

<td align="left"><b>Zip Code</b></td>

<td align="left"><b>Bedrooms</b></td>

<td align="left"><b>Bathrooms</b></td>

<td align="left"><b>Stories</b></td>

<td align="left"><b>Lot Size</b></td>

<td align="left"><u><b>Price</b></u></td>

<td align="left"><b>Detail</b></td>

</tr><p></p>

 

';

 

//Get and print the records

 

$bg='ffffff'; //Set the background color

 

while ($row=mysql_fetch_array

($result, MYSQL_ASSOC)) {

 

$bg = ($bg=='#ffffff' ? '#eeeeee' : '#ffffff'); // Switch the background color.

echo '<tr bgcolor="' . $bg . '">

    <td align="left">' . $row['address'] . '</td>

    <td align="left">' . $row['location'] . '</td>

    <td align="left">' . $row['zip'] . '</td>

    <td align="left">' . $row['bedroom'] .  '</td>

    <td align="left">' . $row['bathroom'] .  '</td>

    <td align="left">' . $row['stories'] .  '</td>

    <td align="left">' . $row['lotsize'] .  '</td>

    <td align="left">' . '$' . $row['price'] .  '</td>

    <td align="left"><a href="detail_home.php?id=' . $row['home_id'] . '">Details</a></td>

  </tr><p></p>

 

  ' ;

 

  }

echo '</table>';

 

} else {

  echo '<p class="error"> Problems<p>';

  echo '<p>' . mysql_error() . ' <br />

  <br />Query: ' .$query . '</p>';

 

 

}

 

mysql_close();

 

}

 

?>

<h2>Register</h2>

<form action="search_homes.php" method="post">

<p>Stories: <input type="text" name="stories" size="25" maxlength="25" /></p>

<p><input type="submit" name="submit" value="Submit" /></p>

<input type="hidden" name="submitted" value="TRUE" />

</form>

<?php

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

?>

Link to comment
https://forums.phpfreaks.com/topic/46908-saerch-form/
Share on other sites

Ok, i figured out it was the connection to a previous db. I did test it and pulls exactly what I wanted.

 

But what I did do was add another field to search on. When I search on the stories it works. When I search on the bedrooms it pulls everything. What I do want it to do is have it search like all other search forms. if it has 2 stories, view it. If it has 3 bedrooms, view it. If it has 2 stories and 2 bedrooms, view it.

 

<?php # Script 7.4 - view_homes.php

 

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

 

echo '<h1 id="mainhead">Homes</h1>';

 

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

 

require_once ('./includes/mysql_connect.php'); // Connect to the db.

 

$st = escape_data($_POST['stories']);

 

$br = escape_data($_POST['bedrooms']);

 

$query = "SELECT * FROM homes WHERE stories = '$st' OR bedroom = '$br' ORDER BY zip ASC ";

$result=mysql_query($query);

 

if ($result) {

 

echo '<table align="center" cellspacing="0" cellpadding="5">

<tr><td align="left"><b>Address</b></td>

<td align="left"><b>Location</b></td>

<td align="left"><b>Zip Code</b></td>

<td align="left"><b>Bedrooms</b></td>

<td align="left"><b>Bathrooms</b></td>

<td align="left"><b>Stories</b></td>

<td align="left"><b>Lot Size</b></td>

<td align="left"><u><b>Price</b></u></td>

<td align="left"><b>Detail</b></td>

</tr><p></p>

 

';

 

//Get and print the records

 

$bg='ffffff'; //Set the background color

 

while ($row=mysql_fetch_array

($result, MYSQL_ASSOC)) {

 

$bg = ($bg=='#ffffff' ? '#eeeeee' : '#ffffff'); // Switch the background color.

echo '<tr bgcolor="' . $bg . '">

    <td align="left">' . $row['address'] . '</td>

    <td align="left">' . $row['location'] . '</td>

    <td align="left">' . $row['zip'] . '</td>

    <td align="left">' . $row['bedroom'] .  '</td>

    <td align="left">' . $row['bathroom'] .  '</td>

    <td align="left">' . $row['stories'] .  '</td>

    <td align="left">' . $row['lotsize'] .  '</td>

    <td align="left">' . '$' . $row['price'] .  '</td>

    <td align="left"><a href="detail_home.php?id=' . $row['home_id'] . '">Details</a></td>

  </tr><p></p>

 

  ' ;

 

  }

echo '</table>';

 

} else {

  echo '<p class="error"> Problems<p>';

  echo '<p>' . mysql_error() . ' <br />

  <br />Query: ' .$query . '</p>';

 

 

}

 

mysql_close();

 

}

 

?>

<h2>Register</h2>

<form action="search_homes.php" method="post">

<p>Stories: <input type="text" name="stories" size="25" maxlength="25" /></p>

<p>Bedrooms: <input type="text" name="bedrooms" size="25" maxlength="25" /></p>

<p><input type="submit" name="submit" value="Register" /></p>

<input type="hidden" name="submitted" value="TRUE" />

</form>

<?php

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

?>

Link to comment
https://forums.phpfreaks.com/topic/46908-saerch-form/#findComment-229169
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.