Jump to content

search box script


paulmo

Recommended Posts

trying about.com search box script on any keyword, getting

Notice: Undefined variable: searching in (line #)

Notice: Undefined variable: find in (line #)

 

seems those variables are defined in code below though. any help appreciated. (i've xxx'd out db connection for posting here):

 

<form name="search" method="post" action="beta.php"> //posting to self
Write something: <input type="text" name="find" /> in 
<Select NAME="field">
<Option VALUE="terms">Terms</option>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

<? 
//This is only displayed if they have submitted the form 
if ($searching =="yes") 
{ 
echo "Results"; 
}
//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("xxx") or die(mysql_error()); 
mysql_select_db("xxx") 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 db WHERE upper($field) LIKE'%$find%'"); 

//And we display the results 
while($result = mysql_fetch_array( $data )) 
{ 
echo $result['terms']; 
echo " "; 
} 
//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; 
} 
?> 

Link to comment
Share on other sites

Actually neither one of those values are actually declared. if you were to put

 

$searching = "yes";

 

above:

//This is only displayed if they have submitted the form 
if ($searching =="yes") 

 

The error would go away. You form does not post to that page it posts to another page so the variable searching is not set a value. Same thing with $find.

Link to comment
Share on other sites

thanks for your help. the search box is only executing from single word search box entries. upon a match, the correct table field row is being echoed, but i need to have an entire sentence searched against table field. please take a look:

<form name="search" method="post" action="beta.php">
<center>Write something: </br></br>
<input type="text" name="find" class="buttonsb"/> 
<input type="hidden" name="searching" value="yes" /></br></br></br>
<input type="submit" name="search" value="Process" class="buttons" />
</center>
</form>
$searching = $_POST['searching'];
$searching = "yes";
if ($searching =="yes") 
{ 
//echo "Results"; 
}
//If they did not enter a search term we give them an error 
$find = $_POST['find'];
//$find = "yes"; 

(if i leave $find = "yes" then the form echoes "yes" instead of matched text, ...was trying to emulate your suggestion with $searching = "yes")

if ($find == "") 
{ 
echo "Please write something."; 
exit; 
} 

// Otherwise we connect to our Database 
mysql_connect("xxx", "xxxx", "xxx) or die(mysql_error()); 
mysql_select_db("xxx") or die(mysql_error()); 

// We perform 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 beta WHERE terms LIKE'%$find%'"); 

WHERE terms (and $result['terms'] below) refers to table field with text; this part is working

//And we display the results 
while($result = mysql_fetch_array($data)) 
{ 
echo $result['terms']; 
echo " "; 
} 
//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 "try again<br><br> "; 
//And we remind them what they searched for 
echo "You wrote: " .$find; 
} 

 

i'll also need to insert the orginal user 'find' into a table field. seems only one possible query to a page. advice?

//mysql_query("INSERT INTO beta (find, created) VALUES ('$find', NOW())")or die(mysql_error());

?>

 

thanks for help. remember i need 1) multiple word user search box match (not just single word) and 2) insert original 'find' variable (text) into a table field. 

Link to comment
Share on other sites

First thing I see is that you need to remove the $searching = "yes". That was just to show you that you did not set the value and how to set it. When you do the $searching = $_POST['searching'] that sets the $searching. That also explains why the $find did not work for. You set it correctly doing $find = $_POST['find'].

 

As far as only matching for the word, it should be working correctly the way you have it. check this out http://www.htmlite.com/mysql011.php. Basically the % at the end tells it to check for anything after the $find.

 

The last statement that you made is incorrect. You can have multiple queries on a page. Actually it is more common than you would think to have multiple queries on one page. If it is not working tell me if you get an error or what happens. I don't understand how you are trying to enter that data in the same table. I would create a separte table for the searched words if I was you.

Link to comment
Share on other sites

As far as only matching for the word, it should be working correctly the way you have it. 

yes it's matching fine for one word, but not working at all for more multiple words. for example, "yellow" in search box echoes a table field row about a yellow sun, etc. but "yellow dog in the road" in search box echoes "try again." i've read the link you provided. possible fixes to this problem?

 

create a separte table for the searched words

ok so 2 separate database connections/queries for 2 different tables on same page?

 

thanks again

 

 

Link to comment
Share on other sites

I meant to say that it will match the phrase. If you enter yellow sun but there is no entry that says yellow sun then it isn't going to find it because it is trying to find the phrase yellow sun. I do not know what you would search for to find any word in a search box. You are better off to post just that question in the mysql forums as they will be better with the queries.

 

As far as the 2 queries goes. You do not need 2 connections for the queries. You only need 1 connection to the database with 2 different queries. For example

 

$conn = mysql_connect("host","username","password");
$select_db = mysql_select_db("database");

$first_query = "SELECT * FROM table";
$first_results = mysql_query($first_query);

$second_query = "INSERT INTO table2 (first_name,last_name) VALUES ('john','doe')";
$second_results = mysql_query($second_query);

 

You do not need to duplicate the connection as long as you are using the same database. If you need to connect to 2 different databases then you would have to connect to that database but there is generally no need for separate databases just tables. you may want to look into database normalization. Basically what that is, is never having 2 of the same values in any table at any time. You will create separate tables with linking values to separate the data. It makes it much more efficient and overall better.

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.