Jump to content

Text Search Not Working


yandoo

Recommended Posts

Hi,

 

I was hoping for a little help with regards to my teaxt search form i have created. Basically i have followed a tutorial to create a table in DB and create the search form the code below is for creating the table:

 

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 bit of code is to create the search form itself:

 

<form name="search" method="post" action="<?php echo $_GET['SearchString']; ?>">

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>

 

 

<p>

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

exit;

}

 

// Otherwise we connect to our Database

mysql_connect("localhost", "root", "winn3rs" ) or die(mysql_error());

mysql_select_db("laptop_loan_database") 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;

}

 

?>

  </p>

<p> </p>

<p> </p>

<p> </p>

<p> </p>

</body>

</html>

 

 

Originally problem was that i did not have permission to access the server - 403 error! This however was resolved by editing the php code in action bit of textfield form to <?php echo $_GET['SearchString']; ?>. The sytax before the cnage was similar except it was like an abreviation of the longer version of syntax. (this could be adjusted accordingly in php.ini file.

 

My problem is that now there is NO 403 error that page is displayed but it does not do anything?? It doesnt search anything, it doesnt display a message when nothing is entered into textfield, it doesnt do anything!!!

 

Please help me and ill be over the moon

 

Many thanks and kind regards.

 

Tom

 

 

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

Try putting this at the top:

 

<?php

 

error_reporting(E_ALL);

ini_set('display_errors', '1');

 

It will help you identify the errors...

 

Also the action in the form seems a bit odd to me... That should be where you are posting the info to...

 

 

Hi,

 

Thanks for your help i tried adding the code you suggested and got the following message:

 

Undefined variable: searching in C:\wamp\www\LaptopLoanDatabase\searchtest.php on line 39 which is this line of code:

 

//This is only displayed if they have submitted the form

if ($searching =="yes")

 

 

When i tried searching for i got this message:

 

The requested URL /laptoploandatabase/<br /><b>Notice</b>: Undefined index: SearchString in <b>C:\wamp\www\LaptopLoanDatabase\searchtest.php</b> on line <b>21</b><br /> was not found on this server. The line of code points to this: <form name="search" method="post" action="<?php echo $_POST['SearchString']; ?>">

 

 

I have also changed the action bit of form to POST instead of GET. Previously when i was getting 403 error the syntax used was: action="<?=$PHP_SELF?>"> as opposed to the now working: <?php echo $_POST['SearchString']; ?>

 

 

Please keep helping i can feel we are close now!

 

thank you kindly :)

Ok the first error is because you are checking the value of something before it even exists ($searchstring does not exist until the page is submitted)

 

Your action is incorrect. Try just hard coding it to searchtest.php for now:

 

action="searchtest.php" and on line 39 change it to

 

if( (isset($_POST['searchstring]')) && ($_POST['searchstring']=="yes") )

Hi,

 

Ive changed to code accordingly and there are NO errors but the search form doesn't find any results when searching any of the fields Or even display message when search field is left empty??????

 

There is certainly records in the tables so its not as though there is nothing to find.

 

 

 

any help much appreciated :)

 

 

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.