Setting up a simple search with PHP5

  RicScott 11:50 17 Sep 06
Locked

I'm trying to set up a simple search that will show only one result..

Basically, someone enters a number and then clicks search, the result should show on another page, picking out the priKey from a mySql database.

I've set up the form, just need help retreiving the data.

  RicScott 10:54 18 Sep 06

The html form:
<form name="search" method="post" action="results.php">
Enter an ID Number: <input type="text" name="find" />

<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

The PHP code on results.php:

<?php
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter an ID Number, Please try again.";
exit;
}

// Connect to DB
require("inc/conn.php");

// Make the query.
$sql = "SELECT * FROM tbl WHERE ID($field) ='%$find%'";

$result = @mysql_query ($sql); // Run the query.

echo '<table align="center" cellspacing="0" cellpadding="5" border="0" width="100%">
';

// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

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

<td align="left" valign="middle"><img src="' . $row['image_link'] .'" width="401px" height="301px"></td>

<td align="left" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td align="left" valign="top" class="style5">Property ID: <b> ' . $row['propertyID'] . '</b><br> Priced From: <b>€ ' . $row['price'] . '</b><br>
Type: <b> ' . $row['type'] . '</b><br>Name: <b>' . $row['name'] . '</b><p>
Area: <b> ' . $row['area'] . '</b><br>
Bedrooms: <b> ' . $row['bedrooms'] . '</b> :: Bathrooms: <b> ' . $row['bathrooms'] . '</b><p>
Pool:<b> ' . $row['pool'] . '</b> :: Garden:<b> ' . $row['garden'] . '</b> :: parking:<b> ' . $row['parking'] . '</b><p><br></td></tr>
<tr><td align="left" valign="top" class="style1">' . $row['description'] . '
</td>
</tr></table>
';
}

echo '</table>';

mysql_free_result ($result); // Free up the resources.
mysql_close(); // Close the database connection.
}
?>

But nothing shows...

  harristweed 11:26 18 Sep 06

To start with:
$sql = "SELECT * FROM tbl WHERE ID($field) ='%$find%'";

Where is $field set?

why not
$sql = "SELECT * FROM tbl WHERE ID ='%$find%'"; ?

also

Remove the @ from
$result = @mysql_query ($sql); // Run the query.
$result = mysql_query ($sql); // Run the query.

then you will see the error message, that might help!

  RicScott 12:04 18 Sep 06

but unfortunately, I still get a blank page..No errors and no programmed errors are returned.

  harristweed 12:54 18 Sep 06

try

echo $sql;

and see what the search string looks like

  RicScott 13:30 18 Sep 06

Still a blank page..this language will be the death of me...

  harristweed 13:48 18 Sep 06

Could be....
if ($searching =="yes")
{
echo "<h2>Results</h2><p>";


where's $searching=$_POST[searching]; ?

Therefore $searching doesn't ==yes
}else{
do nothing!
}
so nothing to do, code works!

  RicScott 16:18 18 Sep 06

Removed the % % from the search which showed a return of -
Results

You forgot to enter an ID number........

Just not showing the result...

I'm getting there.

This thread is now locked and can not be replied to.

Huawei P10 review

1995-2015: How technology has changed the world in 20 years

An overview: What leading creative agencies are doing to improve diversity

New iPad, iPhone SE & Red iPhone 7 on sale now