I've four search fields that are utilized to search a database for book id:s after which echos the result. Based on wich area you decide to search in the sql query accumulates as you can tell within the code below. The title and isbn area will work fine however when I use the writer or category area nothing will get came back. The relevent database tables may also be seen below. Maybe thereĀ“s a problem with generate an income make use of the sql function LIKE???

Database:

CREATE TABLE IF NOT EXISTS `bok` (
  `bokId` int(11) NOT NULL AUTO_INCREMENT,
  `bokTitel` varchar(100) DEFAULT NULL,
  `upplaga` varchar(100) DEFAULT NULL,
  `ISBN` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`bokId`)
)

CREATE TABLE IF NOT EXISTS `skrivenav` (
  `bokId` int(11) DEFAULT NULL,
  `fId` smallint(6) DEFAULT NULL
) 

CREATE TABLE IF NOT EXISTS `forfattare` (
  `fId` smallint(6) NOT NULL,
  `fNamn` varchar(80) DEFAULT NULL,
  PRIMARY KEY (`fId`)
) 

CREATE TABLE IF NOT EXISTS `bokkat` (
  `bokId` int(11) DEFAULT NULL,
  `katId` smallint(6) DEFAULT NULL
) 

CREATE TABLE IF NOT EXISTS `kategori` (
  `katId` smallint(6) NOT NULL,
  `katNamn` varchar(80) DEFAULT NULL,
  PRIMARY KEY (`katId`)
) 

PHP code:

<?php    
$q = "SELECT DISTINCT bokId FROM ";
if($_GET['search_title']!=""||$_GET['search_ISBN']!=""){
    $q = $q."(SELECT * FROM bok WHERE ";
    if($_GET['search_title']!="")   
        $q = $q."bokTitel LIKE '%$_GET[search_title]%' ";
    if($_GET['search_title']!="" && $_GET['search_ISBN']!="")
        $q = $q."AND ";
    if($_GET['search_ISBN']!="")    
        $q = $q."ISBN LIKE '%$_GET[search_ISBN]%' ";
    $q = $q.") AS F";
}
else $q = $q."bok";                         
if($_GET['search_author']!=""){
    $author = explode(",", $_GET['search_author']);
    $auth = "";
    foreach ($author as $value){
        $auth = $auth . "%" . $value . "%', '";
    }
    $auth = trim($auth, ", '"); 
    $q = $q." NATURAL JOIN (SELECT * FROM skrivenav NATURAL JOIN forfattare WHERE fNamn LIKE ('$auth')) AS S ";
}
if($_GET['search_category']!="") {
    $category = explode(",", $_GET['search_category']);
    $cat = "'";
    foreach ($category as $value){
        $cat = $cat . "%" . $value . "%', '";
    }
    $cat = trim($cat, ", '");
    $q = $q." NATURAL JOIN (SELECT * FROM bokkat NATURAL JOIN kategori WHERE katNamn LIKE ('$cat')) AS K ";
}
$rs = mysql_query($q);
confirm_query($rs);    
while($row = mysql_fetch_row($rs)){
    echo $row[0]."<br />";
}
?>

Produced query when searching with author area: Choose DISTINCT bokId FROM bok NATURAL JOIN (Choose * FROM skrivenav NATURAL JOIN forfattare WHERE fNamn LIKE ('%Jonas%', '%Alex%')) AS S

Quick solution from anthares answer also it labored so thanks!!

if($_GET['search_author']!="")

I believe this bit of code:

$author = explode(",", $_GET['search_author']);

$auth = "";

$q = $q.    " NATURAL JOIN (SELECT * FROM skrivenav NATURAL JOIN forfattare WHERE fNamn LIKE ";

foreach ($author as $value){

    $auth = $auth . "%" . $value . "%'";

    $q = $q.    "'$auth OR ";

    $auth = "";

}

$q = trim($q, " OR");

$q = $q.    ") AS A";

will return an effect only when you pass as value subset from the real authors in exact order that is written. Which means this query does not look for scrambled authors' names.

Exactly the same factor with groups.

You need to add an "or" inside your where clause for each category or author inside your filter making a separate expression for all of them.

perhaps you have attempted to appear the particular produced query?