如何创建多个单词搜索? SQL

We have made a search field where you can search for ingredients and find recipes. For now you can only type in 1 ingredient:

if (isset($_POST['search'])) {
$searchquery = $_POST['search'];


$query = mysql_query("SELECT * FROM opskrifter WHERE id IN 
(SELECT opskrifterid FROM ingredienser WHERE ing_name IN ('$searchquery'))") or die("search failed");

We want to be able to search for multiple ingredients in the same search field by seperating the ingredients with a "," or something like this.

Is there a simple way to make that happen ?

EDIT: We tried to use explode like this without succes.

$searchTerms = explode(' ', $searchquery);
$searchTermBits = array();
foreach ($searchTerms as $term) {
if (!empty($term)) {
    $searchTermBits[] = "ing_name '$term'";
}}

...
$result = mysql_query("SELECT * FROM opskrifter WHERE id IN (SELECT * FROM  WHERE ".implode(' AND ', $searchTermBits)));

Thanks! :)

You could simply get the user to type in his values comma-separated, the the input would be almost in the right syntax for the query. You just have to add semicolons around the values because you search for a string in your table.

You can use PHP's str_replace()-Function:

$vals = $_POST['search'];
$valsFormatted = "'" . str_replace(",", "','", $vals) . "'";

In this code, you replace all the commas of the input with the comma plus semicolons before and behind them in orderto wrap all values of the input with semicolons. You also have to add one at the beginning and at the end of the string. Replace the first comma in the function above with the char you want your users to separate the values with.

After that, you can simply change your query to the following:

$query = "SELECT * FROM opskrifter WHERE id IN 
(SELECT opskrifterid FROM ingredienser WHERE ing_name IN ('$valsFormatted'))";

Please also be informed, that your code like this is vulnerable for SQL Injections! Check out this link to learn how to prevent this.

A simple statement like this would work:

$array = implode("','",explode($_POST['search'], ","));
$query = mysql_query("SELECT * FROM opskrifter WHERE id IN (SELECT opskrifterid FROM ingredienser WHERE ing_name IN ({$array}))") or die("search failed");

First explode your search, then implode it (might not even need to do so). After that make sure the array gets used as the 'in' operator as a string/array.

For more information about this, you could read this question: PHP/MySQL using an array in WHERE clause


The working copy from my local machine was this;

$_POST['search'] = "0, 1, 2";

$array = implode ( "','", explode ( ",", $_POST['search'] ) );
$query = mysql_query("SELECT * FROM users WHERE id IN ('$array')") or die(mysql_error());
var_dump ( $array );
var_dump ( $query );
var_dump ( "SELECT * FROM users WHERE id IN ('$array')" );
var_dump ( mysql_fetch_array ( $query ) );

which actually did return users, so if we would take this example and change it to your code, it would be (the query, at least):

$query = mysql_query("SELECT * FROM opskrifter WHERE id IN (SELECT opskrifterid FROM ingredienser WHERE ing_name IN ('$array'))") or die(mysql_error());

Do take note of the changed $array variable too.

First you need to convert the text coming from the search field to array with:

 $string = $_POST['search'];
 $array = explode( '"' , $string); 

So if you put in the search: test"hello"hi

the array will be:

1 => test,
2 => hello,
3 => hi

After that, you need to use the SQL format:

WHERE column_name IN (value1,value2,...)

So you need to change the array we have created to a string with this format:

$string = implode(',',$array);

So the echo of $string will be:

test,hello,hi

and SQL will be :

WHERE column_name IN ($string)