Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 625889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:21:34+00:00 2026-05-13T19:21:34+00:00

I have four search fields that are used to search a database for book

  • 0

I have four search fields that are used to search a database for book id:s and then echos out the result. Depending on wich field you choose to search from the sql query builds up as you can see in the code below. The title and isbn field are working fine but when I try to use the author or category field nothing gets returned. The relevent database tables can also be seen below. Maybe there´s something wrong with the way I use 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 />";
}
?>

Generated query when searching with author field:
SELECT DISTINCT bokId
FROM bok
NATURAL JOIN (SELECT *
FROM skrivenav
NATURAL JOIN forfattare
WHERE fNamn LIKE (‘%Jonas%’, ‘%Alex%’)) AS S

Quick solution from anthares answer and it worked so thank you!!

if($_GET[‘search_author’]!=””){

$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";

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:21:34+00:00Added an answer on May 13, 2026 at 7:21 pm

    I think this piece of code:

    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 ";
    

    will return a result only if you pass as value subset of the real authors in exact order which is written. So this query doesn’t check for scrambled authors’ names.

    The same thing with categories.

    You should add an “or” in your where clause for every category or author in your filter and make a separate expression for each of them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have four tables in my database and i want to join that's and
I have a page where user is provided with four search fields. I am
i have four tables user-question contains two columns: questionID, userID, the questions that the
i have four imageviews in a linear layout in such a manner that only
I currently have four change() events that fire exactly the same code (see below),
I am trying to create a query that will search for fields in a
I have a form with 4 groups of checkboxes used to refine search results.
With out making a database backend, is it possible to somehow create a search
I have four text fields for FirstName , LastName , ID , and department
I have a small doubt here... If I know that a search element in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.