Jump to content

why the code can't search


runeveryday

Recommended Posts

this is the creation table code.the database is testing.

CREATE TABLE users (fname VARCHAR(30), lname VARCHAR(30), info BLOB); INSERT INTO users VALUES ( "Jim", "Jones", "In his spare time Jim enjoys biking, eating pizza, and classical music" ), ( "Peggy", "Smith", "Peggy is a water sports enthusiast who also enjoys making soap and selling cheese" ),( "Maggie", "Martin", "Maggie loves to cook itallian food including spagetti and pizza" ),( "Tex", "Moncom", "Tex is the owner and operator of The Pizza Palace, a local hang out joint" )

 

the html code:

<h2>Search</h2> 
<form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
Seach for: <input type="text" name="find" /> in 
<Select NAME="field"> 
<Option VALUE="fname">First Name</option> 
<Option VALUE="lname">Last Name</option>
<Option VALUE="info">Profile</option> 
</Select> 
<input type="hidden" name="searching" value="yes" /> 
<input type="submit" name="search" value="Search" /> 
</form>

 

 

the search code:

<?php
if ($searching =="yes") { 
echo "<h2>Results</h2><p>";
if ($find == "") { 
echo "<p>You forgot to enter a search term"; 
exit; 
} 
mysql_connect("localhost", "root", "123") or die(mysql_error()); 
mysql_select_db("database_name") or die(mysql_error()); 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 
$data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'"); 
while($result = mysql_fetch_array( $data )) { 
echo $result['fname']; 
echo " "; 
echo $result['lname']; 
echo "<br>"; 
echo $result['info']; 
echo "<br>"; 
echo "<br>"; 
} 
$anymatches=mysql_num_rows($data);
if ($anymatches == 0) { 
echo "Sorry, but we can not find an entry to match your query<br><br>"; 
} 
echo "<b>Searched For:</b> " .$find; 
} 
?> 

Link to comment
https://forums.phpfreaks.com/topic/217539-why-the-code-cant-search/
Share on other sites

If that is everything you have in your search script ("code"), $field and $find have no values. You'll need to add this to the beginning of the script:

 

$field = $_POST['field'];
$find = $_POST['find'];

 

If that isn't what's wrong, tell us what doesn't work. Does the query fail, or are no results returned?

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.