# Search with multiply words

**URL:** <https://www.phphelp.com/t/search-with-multiply-words/21426>\
**Category:** Code Snippets\
**Created:** [November 1, 2013, 3:38pm UTC](https://www.phphelp.com/t/search-with-multiply-words/21426 "2013-11-01T15:38:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://www.phphelp.com/uploads/default/original/2X/2/243e9ed5d93c5653ce3186f18df16e019ce38445.png) [@system](https://www.phphelp.com/u/system)\
**Post date:** [November 1, 2013, 3:38pm UTC](https://www.phphelp.com/t/search-with-multiply-words/21426/1 "2013-11-01T15:38:42Z")

</div>

I’m about to update my search script. The search engine searches in my database where I ask it to search. What is new is I want do some search words, that the users can check in a checkbox, if he wants to use the word in the search.

As for now my search engine works, the only problem is that it only searches the last word and not all of the checked words. My formula looks like this:

[php]\<form method=“POST” action="\<?=$\_SERVER["PHP\_SELF"]?\>"\>

Search for:

 Books: Movies: Outdoor: Indore: [/php]

The php code looks like:  
[php]\<?php  
if(isset($\_POST[‘search’]))  
{  
$connx = mysql\_connect(‘localhost’, ‘_ **’, ',** _\*\*’) or die(“connx”);  
$db = mysql\_select\_db(’\*\*\*\*\*\*\*\*\*’) or die(mysql\_error());

# convert to upper case, trim it, and replace spaces with “|”:

$search = mysql\_real\_escape\_string($search);  
$search = strtoupper(preg\_replace(’/\s+/’, ‘|’, ($\_POST[‘search’])));

# create a MySQL REGEXP for the search:

$regexp = “REGEXP ‘[:\<:][[:\>:]]’”;  
$query = "SELECT \* FROM `keywords` WHERE UPPER(`keywords01`) $regexp OR ".  
"`keywords02` $regexp OR ".  
"`keywords03` $regexp OR “.  
"`keywords04` $regexp”;

$result = mysql\_query($query) or die($query . " - " . mysql\_error());

echo “

\n”;  
while($row = mysql\_fetch\_assoc($result))

{  
echo “

”;  
echo “”;  
echo “”;  
echo “”;  
echo “”;  
echo “\n”;  
}  
}  
else {  
echo “

Sorry, no results matched your search.

”;  
}  
?\> [/php]

Are there someone, who can figure out, why it is only the last marked checkboxs word that are searched and not all marked words and how do I get it to search for all marked words?

Hope someone can help.

| \<img src=…/{$row[‘type’]}/{$row[‘folder’]}/{$row[‘date’]}-{$row[‘num’]}/{$row[‘thumbimage’]} border=1\> | {$row[‘name’]} | {$row[‘date’]} | \<a href=…/view.php?id={$row[‘id’]} target=blank\>VIEW |

---

<div class="post-metadata">

**Author:** ![Topcoder](https://www.phphelp.com/user_avatar/www.phphelp.com/topcoder/32/4356_2.png) [@Topcoder](https://www.phphelp.com/u/Topcoder)\
**Post date:** [November 3, 2013, 4:53pm UTC](https://www.phphelp.com/t/search-with-multiply-words/21426/2 "2013-11-03T16:53:18Z")

</div>

Here’s what I think you have to do…

Since you’re using the same “name” for all your check boxes you need to read it from an array and define it as such…

[php]Books:   
Movies:   
Outdoor:   
Indore: [/php]

Then do a

[php]print\_r($\_POST[‘search’])  
[/php]

To see all your values.

Or you can take the easy way out and just do something like this…

[php]\<form method=“POST” action="\<?=$\_SERVER["PHP\_SELF"]?\>"\>

Search for:

 Books: Movies: Outdoor: Indore: [/php]

[php] \<?php  
if(isset($\_POST[‘search’]))  
{  
$connx = mysql\_connect(‘localhost’, ‘_ **’, ',** _\*\*’) or die(“connx”);  
$db = mysql\_select\_db(’\*\*\*\*\*\*\*\*\*’) or die(mysql\_error());

```
# convert to upper case, trim it, and replace spaces with "|": 
$search = mysql_real_escape_string($search); 
$search .= strtoupper(preg_replace('/\s+/', '|', ($_POST['searchbooks']))); 
$search .= strtoupper(preg_replace('/\s+/', '|', ($_POST['searchmovies']))); 

```

$search .= strtoupper(preg\_replace(’/\s+/’, ‘|’, ($\_POST[‘searchoutdoor’])));  
$search .= strtoupper(preg\_replace(’/\s+/’, ‘|’, ($\_POST[‘searchindore’])));  
$search .= strtoupper(preg\_replace(’/\s+/’, ‘|’, ($search)));

# create a MySQL REGEXP for the search:

$regexp = “REGEXP ‘[:\<:][[:\>:]]’”;  
$query = "SELECT \* FROM `keywords` WHERE UPPER(`keywords01`) $regexp OR ".  
"`keywords02` $regexp OR ".  
"`keywords03` $regexp OR “.  
"`keywords04` $regexp”;

$result = mysql\_query($query) or die($query . " - " . mysql\_error());

echo “

\n”;  
while($row = mysql\_fetch\_assoc($result))

{  
echo “

”;  
echo “”;  
echo “”;  
echo “”;  
echo “”;  
echo “\n”;  
}  
}  
else {  
echo “

Sorry, no results matched your search.

”;  
}[/php]

| \<img src=…/{$row[‘type’]}/{$row[‘folder’]}/{$row[‘date’]}-{$row[‘num’]}/{$row[‘thumbimage’]} border=1\> | {$row[‘name’]} | {$row[‘date’]} | \<a href=…/view.php?id={$row[‘id’]} target=blank\>VIEW |
