0

I have a database created in phpmyadmin, with few columns and data inserted.Right now i can display the table with record in the browser. But what i want is to display a particular value by filtering from the database.the following is the piece of code i have written. At this moment if I input any value into the search field it doesn't sort out, and do nothing.

        <?php
    if(isset ($_POST['search']))
    {
        $valueToSearch=$_POST['valueToSearch'];
        //$query="SELECT * FROM `books` WHERE CONCAT (`ISBN`, `Title`, `Author`)LIKE '%".$valueToSearch."%'";
        $query="SELECT * FROM books WHERE CONCAT ('ISBN', 'Title', 'Author')LIKE '%".$valueToSearch."%'";
        $serach_result=filterTable($query);
    }else{
        $query="SELECT * FROM books";
        $serach_result=filterTable($query);
    }

    function filterTable($query){

        $connect=mysqli_connect ("localhost", "root", "", "bookstore-new");
        $filter_result=mysqli_query($connect, $query);
        return $filter_result;
  }
    ?>
  <!DOCTYPE html>

    <html>
    <head>

    <title>Lab 4</title>

    <style>
    table, tr, th, td 
    {
        border: 1px solid black;
    }
    </style>
</head>
  <body>
    <form action="bookstorewebdev.php" method="post">

    <input type="text" name="valueToSearch" placeholder="value to search" >
    <br><br>
  <input type="submit" name="Search" value="Filter" ><br><br>
   <table>
    <tr>
    <th>ISBN</th>
    <th>Title</th>
    <th>Author</th>
  </tr>
      <?php while ($row=mysqli_fetch_array($serach_result)):?>
 <tr>
    <td>&nbsp;<?php echo $row['ISBN'];?></td>
    <td>&nbsp;<?php echo $row['Title'];?></td>
    <td>&nbsp;<?php echo $row['Author'];?></td>
    </tr>
  <?php endwhile;?>

  </table>
 </form>
 </body>
    </html>
5
  • 1
    Are 'ISBN', 'Title', 'Author' columns? Current code is concating that as a string so you are comparing ISBNTitleAuthor. This also is open to SQL injections as well. Commented May 13, 2016 at 17:57
  • You should add validations in your code such as what is shown in the PHP manual: if (!mysqli_query($connect, "YOUR SQL")) { printf("Errormessage: %s\n", mysqli_error($connect)); } Commented May 13, 2016 at 18:01
  • Can you post database table screenshot ? Commented May 13, 2016 at 18:23
  • Furqan Aziz, actually because of the current reputation level i cant add any image. However, can u plese see my new issue that I have commented against UserName's answer. thank u. Commented May 13, 2016 at 21:34
  • chris85, i think u pointed out a good point here. but i havent got clearly. can u be a bit more specific? Ya, they r the columns. Is having Concat a problem? Commented May 13, 2016 at 21:37

1 Answer 1

3

In your html form have:

<input type="submit" name="Search" value="Filter">

Search variable, but in php you check search. Search != search


You do not check the variable valueToSearch. It is can be empty. Request will fail.


About concat said chris85. Why do you need this?

Sign up to request clarification or add additional context in comments.

1 Comment

UserName, thank u, there I had mistake, it works now. the only remaining problem is if i hit filter button more than once, it then display the whole table again. but it is supposed to show only the filtered result, no matter how many times i press the filter button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.