Multiply word search

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 660 Views - Last Post: 01 November 2013 - 07:09 AM Rate Topic: -----

#1 GertK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 31-October 13

Multiply word search

Posted 31 October 2013 - 01:26 PM

I have made a simple search engine that can search my database. It works this way: you check the word you want to search for in a checkbox. Result are sent using a form. The issue is that it only searches for the last word checked. This mean do I check 3 words itís only showing results for the last word checked. Here is my form:

<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>"> 
<p>Search for: 
</p> 
Books: <input type="checkbox" name='search[]' value="Books"> 
Movies: <input type="checkbox" name='search[]' value="Movies"> 
Outdoor: <input type="checkbox" name='search[]' value="Outdoor"> 
Indoor: <input type="checkbox" name='search[]' value="Indoor"> 
</p> 
<p><input type='submit'  value='Search'></p> 
</form> 



This is my codes that generate the result:
    <?php 
if(isset($_POST['search'])) 
{ 
  $connx = mysql_connect('localhost', 'USER', 'PASSWORD') or die("connx"); 
  $db = mysql_select_db('DB_NAME') or die(mysql_error()); 

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

  # create a MySQL REGEXP for the search: 
  $regexp = "REGEXP '[[:<:]]($search)[[:>:]]'"; 
  $query = "SELECT * FROM `galleries` WHERE UPPER(`keywords1`) $regexp OR ". 
           "`keywords2` $regexp"; 
  $result = mysql_query($query) or die($query . " - " . mysql_error()); 

echo "<table>\n"; 
while($row = mysql_fetch_assoc($result)) 

{ 
    echo "<tr>"; 
    echo "<td><img src=../thumbs/{$row['type']}/{$row['folder']}/{$row['date']}-{$row['num']}/{$row['thumbimage']} border=1></td>";
    echo "<td>{$row['name']}</td>"; 
    echo "<td>{$row['date']}</td>"; 
    echo "<td><a href=../view.php?id={$row['id']} target=blank>View</a></td>"; 
    echo "</tr>\n"; 
  }  
} 
?> 



Can someone help me telling me how do I get the search engine to search and/or show more than 1 word searched result?

Is This A Good Question/Topic? 0
  • +

Replies To: Multiply word search

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Multiply word search

Posted 31 October 2013 - 07:52 PM

Keep in mind that when you name your inputs like: name='search[]', the resulting $_POST["search"] element will be an array containing all the input values you selected. Your code treats the element as a single string value, not an array. You need to loop through the values, or perhaps use the implode() function to combine them into a single string. In the context of a regular expression, combining them with the or operator(|) could make sense.

Also, on line 11 you use $_POST["search"] directly to set $search, completely negating everything lines 8 through 10 do. Are you sure you didn't meant to use $search on line 11 instead of $_POST["search"]?
Was This Post Helpful? 0
  • +
  • -

#3 GertK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 31-October 13

Re: Multiply word search

Posted 01 November 2013 - 12:31 AM

I see. I have tried to change the code to:

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



And the form to:
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">   
<p>Search for:   
</p>   
Books: <input type="checkbox" name='search' value="Books">   
Movies: <input type="checkbox" name='search' value="Movies">   
Outdoor: <input type="checkbox" name='search' value="Outdoor">   
Indoor: <input type="checkbox" name='search' value="Indoor">   
</p>   
<p><input type='submit'  value='Search'></p>   
</form> 



And its still not working. Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,441
  • Joined: 08-June 10

Re: Multiply word search

Posted 01 November 2013 - 02:32 AM

what version of PHP are you using? as of PHP 5.4 magic_quotes_gpc is removed completely.
Was This Post Helpful? 0
  • +
  • -

#5 GertK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 31-October 13

Re: Multiply word search

Posted 01 November 2013 - 03:04 AM

Thanks for taking time to answer me. I using: PHP version 5.2.17
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,441
  • Joined: 08-June 10

Re: Multiply word search

Posted 01 November 2013 - 03:16 AM

then you can safely remove line #2/#3.

of your HTML, only the last text field will be received in PHP.
Was This Post Helpful? 0
  • +
  • -

#7 GertK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 31-October 13

Re: Multiply word search

Posted 01 November 2013 - 03:46 AM

Thanks - I have done that now. The PHP code now looks like:
<?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 '[[:<:]]($search)[[:>:]]'"; 
  $query = "SELECT * FROM `galleries` WHERE UPPER(`keywords57`) $regexp OR ". 
           "`keywords58` $regexp OR ". 
           "`keywords59` $regexp OR ". 
           "`keywords60` $regexp"; 
  
  $result = mysql_query($query) or die($query . " - " . mysql_error()); 
  
echo "<table>\n"; 
while($row = mysql_fetch_assoc($result)) 

{ 
    echo "<tr>"; 
	echo "<td><img src=../thumbs/{$row['type']}/{$row['folder']}/{$row['date']}-{$row['num']}/{$row['thumbimage']} border=1></td>";
    echo "<td>{$row['name']}</td>"; 
    echo "<td>{$row['date']}</td>"; 
    echo "<td><a href=../viewgallery.php?id={$row['id']} target=blank>View</a></td>"; 
    echo "</tr>\n"; 
  }  
} 
 else {
  echo "<p>Sorry, no results matched your search.</p>";
}
?> 



And the form:
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">     
<p>Search for:     
</p>     
Books: <input type="checkbox" name='search' value="Books">     
Movies: <input type="checkbox" name='search' value="Movies">     
Outdoor: <input type="checkbox" name='search' value="Outdoor">     
Indoor: <input type="checkbox" name='search' value="Indoor">     
</p>     
<p><input type='submit'  value='Search'></p>     
</form> 




But it still dont Work. Any advice?
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,441
  • Joined: 08-June 10

Re: Multiply word search

Posted 01 November 2013 - 03:50 AM

line #8, where do you define $search?

the HTML code still has the same problem.
Was This Post Helpful? 0
  • +
  • -

#9 GertK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 31-October 13

Re: Multiply word search

Posted 01 November 2013 - 04:17 AM

You mean that we dont need that line?

I was told to use this:
  $search = mysql_real_escape_string($search);  


But is it that line that make the issue?
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,441
  • Joined: 08-June 10

Re: Multiply word search

Posted 01 November 2013 - 04:25 AM

Quote

You mean that we dont need that line?

I never said that.

what I mean is: where does the $search variable come from?
Was This Post Helpful? 0
  • +
  • -

#11 GertK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 31-October 13

Re: Multiply word search

Posted 01 November 2013 - 04:39 AM

Ok, Iím new to PHP, and most of the codes in this script I found elsewhere and put it together and hope to get it working the way I want. This Means that some of the codes I have tried to write myself and its working but when it gets as complex as now I have no idea what to do - thatís why I hope to get some help so I can learn it :)

Hope you can/will advice.
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,441
  • Joined: 08-June 10

Re: Multiply word search

Posted 01 November 2013 - 04:45 AM

advice #1, re-read Atli’s post about naming form elements (post #2)

advice #2, your $search is not defined and how can you escape a value that doesn’t exist?

This post has been edited by Dormilich: 01 November 2013 - 04:46 AM

Was This Post Helpful? 0
  • +
  • -

#13 GertK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 31-October 13

Re: Multiply word search

Posted 01 November 2013 - 05:52 AM

I do understand that part with [] but when Iadd that nothing happens. ITs like my PHP codes dont know what that Means.

If I change it all to:
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">     
<p>Search for:     
</p>     
Books: <input type="checkbox" name='search[]' value="Books">     
Movies: <input type="checkbox" name='search[]' value="Movies">     
Outdoor: <input type="checkbox" name='search[]' value="Outdoor">     
Indoor: <input type="checkbox" name='search[]' value="Indoor">     
</p>     
<p><input type='submit'  value='Search'></p>     
</form> 



Then I dont know what to do, that leads me to you Advice #2. I understand that the $search dosent exist, but how do I get around this then? I really have no idea how to code this part.
Was This Post Helpful? 0
  • +
  • -

#14 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,441
  • Joined: 08-June 10

Re: Multiply word search

Posted 01 November 2013 - 06:27 AM

Quote

I do understand that part with [] but when Iadd that nothing happens.

thatís because your code isnít designed to work with input arrays.

besides that, Iím wondering what your SQL query does. it looks awfully complicated to me.
Was This Post Helpful? 0
  • +
  • -

#15 GertK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 31-October 13

Re: Multiply word search

Posted 01 November 2013 - 07:07 AM

The $Query tells the script where to search in the database. It will be a big database then each search word has its on column that will be attached to each row. That part is allready working fine. I just need to figure out how I get the search codes to search more then that last marked Word in the form.

Can you help me with that?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2