Jump to content

Recommended Posts

Hi all,

i was really hoping for some help on text searching.

 

I have followed tutorial to build a simple searching form that searches records in a DB and displays them accordingly.

 

The following code was used to create the table in the DB:

 

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 next part of code is what builds the search form:

 

<h2>Search</h2>

<form name="search" method="post" action="<?=$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 final bit of code is Search Code:

 

<?

//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 a search term";

exit;

}

 

// Otherwise we connect to our Database

mysql_connect("mysql.yourhost.com", "user_name", "password") or die(mysql_error());

mysql_select_db("database_name") or die(mysql_error());

 

// We preform a bit of filtering

$find = strtoupper($find);

$find = strip_tags($find);

$find = trim ($find);

 

//Now we search for our search term, in the field the user specified

$data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'");

 

//And we display the results

while($result = mysql_fetch_array( $data ))

{

echo $result['fname'];

echo " ";

echo $result['lname'];

echo "<br>";

echo $result['info'];

echo "<br>";

echo "<br>";

}

 

//This counts the number or results - and if there wasn't any it gives them a little message explaining that

$anymatches=mysql_num_rows($data);

if ($anymatches == 0)

{

echo "Sorry, but we can not find an entry to match your query<br><br>";

}

 

//And we remind them what they searched for

echo "<b>Searched For:</b> " .$find;

}

?>

 

 

 

Initially ifound i got an eror saying i do not have permission to view this page. As previously advised i made the following changes to action attribute of the search form:

 

FROM........ action="<?=$PHP_SELF?>">

 

TO........... action="<?php echo $_GET['SearchString']; ?>"

 

The above amendment make it work....Well at least No error was present!!!

 

My problem now is that the search form may not produce any errors but it does not also find any records either?????? I know i have records in the newly constructed table as well!

 

 

Whats going on? Please help me i'm itching to get this done!

 

Thanks & kind Regards :)

 

 

Link to comment
https://forums.phpfreaks.com/topic/50642-text-search-code-not-working/
Share on other sites

Before you get too head deep into this, you may want to consider full text indexing.

 

http://dev.mysql.com/doc/en/Fulltext_Search.html

 

http://en.wikipedia.org/wiki/Full_text_search

 

Way more versatile.

 

You are also assuming that Register_globals is ON this is not good.

 

Reference variables coming in from a form like this initially

 

<?php
$field = $_POST['field'];

 

To avoid certain security breaches.

Hi,

 

i have made the changes you suggested and i get an error saying still not found:

 

The requested URL /local_sites/laptoploandatabase/<br /><b>Notice</b>: Undefined index: SearchString in <b>C:\wamp\www\local_sites\LaptopLoanDatabase\searchtest.php</b> on line <b>15</b><br /> was not found on this server.

 

 

If i add

 

$field = $_POST['field']; etc.. i get an additional Notice error saying:

 

Notice: Undefined index: field in C:\wamp\www\local_sites\LaptopLoanDatabase\searchtest.php on line 33

 

Please help me as ive have been on this specific problem for ages now and cant move on until its resolved.

 

Many thanks

 

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.