Jump to content

PHP Search Feature Problem


faizanafzal

Recommended Posts

Hello Guys,

 

I am just a beginner in PHP. I am learning PHP from tutorials. I just tried to make a simple PHP search feature and connect it to my database to search the keywords from my database table. The tutorial is according to the PHP Version 4. But, my hosting server consist of PHP version 5. I think thats why it is not working. When I am trying to submit the keyword to search through an html form. It is showing me all the keywords from the table. But, it should match up with only that keyword, which i am entering in the html form.

Kindly, help me out in this. Here is my PHP and HTML code.

 

HTML CODE:

 

<form action="result.php" method="POST">

 

<input type="text" name="name"><BR><BR>

 

<input type="submit" value="Search">

 

</form>

 

 

PHP CODE:

 

 

 

 

<?

 

if (!$name) {

 

echo "No Search Result";

exit;

}

 

 

$db = mysql_connect("localhost", "sqlusername", "sqlpassword");

mysql_select_db("sqldatabase", $db);

 

$query = "SELECT people.name, people.Height, people.Date

FROM people WHERE name LIKE '%".$name."%'";

 

$result = mysql_query($query);

 

while ($record = mysql_fetch_assoc($result)) {

 

while ( list($fieldname, $fieldvalue) = each($record)) {

 

 

echo $fieldname . ": <b>" . $fieldvalue . "</b><br>";

 

}

echo "<br>";

 

}

 

?>

Link to comment
https://forums.phpfreaks.com/topic/170050-php-search-feature-problem/
Share on other sites

Not sure if register_globals was on in php4, but you should use the posted variables like this

<?php
if (isset($_POST['name']) && trim(strlen($_POST['name'])))
{
   // do db query and show results
}
else
{
   // Search word was empty or not posted, show some message.
}

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.