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
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?

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.