简体   繁体   中英

Get Results of a search form- MySQL PHP

I have a joined table and it gives all books of my database. And all books are displaying properly. But I need to work it according to search queries entered in form. this is my query for join.

$rs = mysqli_query($connection,"SELECT DISTINCT bk.title As Title, YEAR(bk.date_released) AS Year, bk.price AS Price, cat.name AS Category, pub.name AS Publisher, aut.name AS Author,co.name AS Cover, cp.count AS Copies
            FROM books bk
            JOIN (SELECT book_id, COUNT(*) as count FROM copies GROUP BY book_id) cp
                ON bk.id = cp.book_id
            JOIN category cat
                ON cat.id = bk.category_id
            JOIN publishers pub
                ON pub.id = bk.publisher_id
            JOIN books_covers bk_co
                ON bk_co.book_id = bk.id
            JOIN covers co
                ON co.id = bk_co.cover_id
            JOIN books_authors bk_aut
                ON bk_aut.book_id = bk.id
            JOIN authors aut
                ON aut.id = bk_aut.author_id
            JOIN books_languages bk_lan
                ON bk_lan.book_id = bk.id
            JOIN languages lan
                ON lan.id = bk_lan.lang_id
            JOIN books_locations bk_loc
                ON bk_loc.book_id = bk.id
            JOIN locations loc
                ON loc.id = bk_loc.location_id
            ORDER BY bk.title ASC
                ");
    $copies = mysqli_query($connection,"SELECT DISTINCT COUNT(copies.book_id) FROM copies INNER JOIN books ON copies.book_id=books.id
        ");
    $dup = mysqli_query($connection,"SELECT book_id, COUNT(*) as count FROM copies GROUP BY book_id");
    $rows_copies = mysqli_fetch_array($copies);
    $rows = mysqli_fetch_assoc($rs);
    $tot_rows = mysqli_num_rows($rs);

And this is my variables of search form

if(!empty($_GET)){
    $title = $_GET['title'];
    $author = $_GET['author'];
    $isbn = $_GET['isbn'];
    $language = $_GET['language'];
    $publisher = $_GET['publisher'];
    $year = $_GET['year'];
    $category = $_GET['category'];
}else{
    $title = "";
    $author = "";
    $isbn = "";
    $language = "";
    $publisher = "";
    $year = "";
    $category = "";
    $language = "";
}

And this is my code for displaying results,

<div class="jumbo">
   <?php if($tot_rows > 0){  ?>
   <?php do { ?>
       <div class="col-md-3">
           <span class="product-image">

                <img src="<?php echo $rows['Cover'] ?>" class="img-thumbnail product-img" alt="">
            </span>
               <ul class="iteminfo">
                    <li><strong>Title: </strong><?php echo $rows['Title'] ?></li>
                    <li><strong>Category: </strong><?php echo $rows['Category'] ?></li>
                    <li><strong>Author: </strong><?php echo $rows['Author'] ?></li>
                    <li><strong>Price: </strong><?php echo $rows['Price']." Rs" ?></li>
                    <li><strong>Publisher: </strong><?php echo $rows['Publisher'] ?></li>
                    <li><strong>Copies: </strong><?php echo $rows['Copies'] ?></li>
                </ul>
        </div>
    <?php } while($rows=mysqli_fetch_assoc($rs)); }else{ ?>
    <?php echo 'No Results'; }?>
   </div>

How I get results only I searched with corresponding search queries. For an example if I search a book called "Romeo Juliet" I need to display that book only

I tried to test the diplay with this code and never succeed

$titlequery = mysqli_query($connection," SELECT * FROM "$rs" WHERE Title = "$title" ");
$rows = mysqli_fetch_assoc($titlequery);

Help me to solve this.

You're trying to execute a subquery, but the $rs variable you are passing in is a resource, not a string. If you set the original query to a variable and pass that in, then it should work:

$sql = "SELECT DISTINCT bk.title As Title, YEAR(bk.date_released) AS Year, bk.price AS Price, cat.name AS Category, pub.name AS Publisher, aut.name AS Author,co.name AS Cover, cp.count AS Copies
        FROM books bk
        JOIN (SELECT book_id, COUNT(*) as count FROM copies GROUP BY book_id) cp
            ON bk.id = cp.book_id
        JOIN category cat
            ON cat.id = bk.category_id
        JOIN publishers pub
            ON pub.id = bk.publisher_id
        JOIN books_covers bk_co
            ON bk_co.book_id = bk.id
        JOIN covers co
            ON co.id = bk_co.cover_id
        JOIN books_authors bk_aut
            ON bk_aut.book_id = bk.id
        JOIN authors aut
            ON aut.id = bk_aut.author_id
        JOIN books_languages bk_lan
            ON bk_lan.book_id = bk.id
        JOIN languages lan
            ON lan.id = bk_lan.lang_id
        JOIN books_locations bk_loc
            ON bk_loc.book_id = bk.id
        JOIN locations loc
            ON loc.id = bk_loc.location_id
        ORDER BY bk.title ASC
            ";
$rs = mysqli_query($connection, $query);
$titlequery = mysqli_query($connection, " SELECT * FROM ({$query}) WHERE Title = '{$title}'");

Also, watch your quotation marks in SQL queries when you need to use PHP quotation marks as string delimiters. PHP will interpret your string of " SELECT * FROM "$rs" WHERE Title = "$title" " as SELECT * FROM , the $rs resource, WHERE Title = , the $title variable, and , but without any concatenation. You'd need to backslash your SQL quotations, like " SELECT * FROM \\"$rs\\" WHERE Title = \\"$title\\" " , so PHP doesn't think you want to end your string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM