为什么即使记录不属于该类别,该记录也会出现

时间:2015-02-06 15:10:06

标签: php mysqli sql-like

大家好,你们可以帮助我解决我的问题。 当我搜索其他类别时,我的其他类别的记录似乎发生了什么。查询显示只有类别正确才会搜索记录。

例如记录:catname:来自我的IT的IT记录是web我的另一个catname是历史记录是History123如果我将历史记录作为我的类别搜索并且在搜索时显示为空白

这是我的代码: 我希望你能帮助我成为一个新手的PHP

       <form method="post">
        <input type="text" name="search" class="searchfield" placeholder="Search for Book Title, Author, ISBN COde or Location">
        <select name="catname" class="searchfield">
      <option>--------Categories--------</option>
       <?php

       $query = "select * from categories";
       $result = $con->query($query) or trigger_error('Wrong SQL: ' . $query . ' Error: ' . $conn->error, E_USER_ERROR);
       $result ->data_seek(0);
       while ($row =$result->fetch_assoc()) 
       {
         # code...
        echo'<option>'.$row['catname'].'</option>';
       }

        ?>
    </select>
    <input type="submit" name="searchmo" value="Search" class="btn btn-info btn-lg">
      <h2 class="sub-header">List of Books</h2>
      <div class="table-responsive">

          <!--author LIKE '%$search%' or bookcode LIKE '%$search%' or location LIKE '%$search%'
              or -->
          <?php
          if(isset($_REQUEST['searchmo']))
          {
            $catname = $con->real_escape_string($_POST['catname']);
            $search = $con->real_escape_string($_POST['search']);
             $query1 = $con->query("select bookname,bookid,bookdesc,location,
              categories.catname,qty,bookcode,copyright,editionnumber,publisher,author 
              from categories inner join books on books.catname = categories.catname
              where  books.bookname LIKE '%$search%' or books.author LIKE '%$search%' or
              books.bookcode LIKE '%$search%' or books.location = '%$search%' or
              books.publisher LIKE '%$search%' 
              and categories.catname = '$catname'") or die(mysqli_errno());
            if($query1->fetch_assoc() == 0)
            {
                echo '<div class="alert alert-warning">No result found.</div>';
            }
            else
            {
            $catname = $con->real_escape_string($_POST['catname']);
            $search = $con->real_escape_string($_POST['search']);
            $query = $con->query("select bookname,bookid,bookdesc,location,
              categories.catname,qty,bookcode,copyright,editionnumber,publisher,author 
              from categories inner join books on books.catname = categories.catname
              where  books.bookname LIKE '%$search%' or books.author LIKE '%$search%' or
              books.bookcode LIKE '%$search%' or books.location = '%$search%' or
              books.publisher LIKE '%$search%' 
              and categories.catname = '$catname'") or die(mysqli_errno());
            //$result ->data_seek(0);
            while($row = $query->fetch_assoc())
            {
          ?>
          <div class="panel panel-info">
            <div class="panel-heading">
            <h3 class="panel-title"><strong>Book Title:<?php echo $row['bookname']?>- ISBN: <?php echo $row['bookcode']?> </strong></h3>
            </div>
              <div class="panel-body" style="line-height:10px;">

                <p>Author: <?php echo $row['author']?></p>
                <p>Publisher: <?php echo $row['publisher']?></p>
                <p>Location: <?php echo $row['location']?></p>
                <p>Quantity: <?php echo $row['qty']?> book's available</p>
                <p>Summary:<?php echo $row['bookdesc']?></p>
              </div>
          </div>

          <?php
          } 
            }
          }
          ?>

1 个答案:

答案 0 :(得分:0)

echo您的查询,以便您可以在更换变量后查看其外观。

您目前的方式LIKE '%$search%'看起来像LIKE '%"searchterm"%'。因为您要提前在字符串周围添加引号。

所以分配给$search,如此:$search = $con->real_escape_string($_POST['search']);

旁注:你应该查看准备好的陈述。