0

我为一个学校项目制作了这个网站,它似乎没有检索数据。我一直在检查它 AGES 并且无法找出它为什么不检索。SQL 有时在 phpMyAdmin 中有效,但在界面中从不有效。

<?php

$con=mysqli_connect("localhost","root","#","book_catalogue");

{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<div id="wrapper">
    <div id="left-col">
        <img src="images/left-img.png" alt="books" />
    </div>
    <div id="header">
        <h1>BOOK CATALOGUE</h1>
    </div>

    <!--Radio buttons that allow user to select the search criteria-->
    <div id="navigation">
        <form name="input" action="indexV4.php" method="get">
                SEARCH BY: ALL<input type="radio" name="books" value="ALL" checked>
                TITLE<input type="radio" name="books" value="TITLE">
                AUTHOR<input type="radio" name="books" value="AUTHOR">
                GENRE<input type="radio" name="books" value="GENRE">
                BOOK TYPE<input type="radio" name="books" value="BOOKTYPE"> 
                <input type="text" name="SEARCH" value="SEARCH">
                <input type="submit" value="GO">
        </form>
    </div>
            <?php
            $Books = $_GET['books'];
            $Search = $_GET['SEARCH'];
                //if the radio button 'TITLE' is selected, this query will run
                if($Books=='TITLE')
                    {
                        $result = mysqli_query($con,"
                        SELECT tbl_books.ISBN, tbl_books.Title, tbl_books.Author, tbl_books.Book_Image, tbl_book_type.Name, tbl_categories.Name FROM tbl_books, tbl_book_type, tbl_categories WHERE (tbl_books.Type_id = tbl_book_type.Type_id) AND (tbl_books.Category_id = tbl_categories.Category_id)
                        WHERE tbl_books.Title LIKE '%$Search%'");
                    }
                    //if the radio button 'AUTHOR' is selected, this query will run
                else if ($Books=='AUTHOR')
                    {
                        $result = mysqli_query($con,"
                        SELECT tbl_books.ISBN, tbl_books.Title, tbl_books.Author, tbl_books.Book_Image, tbl_book_type.Name, tbl_categories.Name FROM tbl_books, tbl_book_type, tbl_categories
                        WHERE (tbl_books.Type_id = tbl_book_type.Type_id) AND (tbl_books.Category_id = tbl_categories.Category_id)
                        WHERE tbl_books.Author LIKE '%$Search%'");
                    }
                    //if the radio button 'GENRE' is selected, this query will run
                else if ($Books=='GENRE')
                    {
                        $result = mysqli_query($con,"
                        SELECT tbl_books.ISBN, tbl_books.Title, tbl_books.Author, tbl_books.Book_Image, tbl_books.Type_id, tbl_books.Category_id FROM tbl_books
                        WHERE tbl_books.Author LIKE '%$Search%'");
                    }
                    //if the radio button 'BOOK TYPE' is selected, this query will run
                else if ($Books=='BOOKTYPE')
                    {
                        $result = mysqli_query($con,"
                        SELECT tbl_books.ISBN, tbl_books.Title, tbl_books.Author, tbl_books.Book_Image, tbl_book_type.Name, tbl_categories.Name FROM tbl_books, tbl_book_type, tbl_categories 
                        WHERE (tbl_books.Type_id = tbl_book_type.Type_id) AND (tbl_books.Category_id = tbl_categories.Category_id)
                        WHERE tbl_book_type.Name LIKE '%$Search%'");
                    }
                    //if 'ALL' is selected, this query will run
                else 
                {
                    $result = mysqli_query($con, "
                    SELECT tbl_books.ISBN, tbl_books.Title, tbl_book_type.Name, tbl_categories.Name FROM tbl_books, tbl_book_type, tbl_categories WHERE (tbl_books.Type_id = tbl_book_type.Type_id) AND (tbl_books.Category_id = tbl_categories.Category_id)
                    WHERE tbl_books.Title OR tbl_books.Author OR tbl_categories.Name OR tbl_book_type.Name LIKE '%$Search%'");
                }
            //while it is retrieving the data, echo it onto the screen
     while($row = mysqli_fetch_array($result,MYSQLI_BOTH))
     {
            ?>
    </div>
    <div id="content">
    <!--Display of search results-->

        <div class="books">
            <img src="book-covers/<?php echo $row['Book_Image']; ?>" alt="<?php echo $row['Title']; ?>" />
            <p class="first"><?php echo $row['Title']; ?></p>
            <p><?php echo $row['Author']; ?></p>
            <p><?php echo $row['ISBN']; ?></p>
            <p><?php echo $row['Type_id']; ?></p>
            <p><?php echo $row['Category_id']; ?></p>
        </div>
    </div>
    <?php
    //close the loop
    }
    ?>
</div>

4

2 回答 2

0

正如 invisal 所说,你不能在一个语句中有 2 WHERE。

我建议更改您的 if 语句内容。代替mysqli_query($con, "SELECT BLA BLA");

做这个:

$sql = "选择 BLA BLA";

然后,在 if 语句创建 $sql 之后,执行以下操作:

$result = mysql_query($con,$sql);

if($result === false) {
  echo 'Error in query [' . $sql . ']' . "\n" . 'Error: ' . mysql_error();
}

然后你会看到你的查询出了什么问题。

编辑:让我试着让这个更清楚:

if($Books=='TITLE')
                    {
                        $sql = "SELECT tbl_books.ISBN, tbl_books.Title, tbl_books.Author, tbl_books.Book_Image, tbl_book_type.Name, tbl_categories.Name FROM tbl_books, tbl_book_type, tbl_categories WHERE (tbl_books.Type_id = tbl_book_type.Type_id) AND (tbl_books.Category_id = tbl_categories.Category_id)
                        WHERE tbl_books.Title LIKE '%$Search%'";
                    }

将您的所有 if/else if 语句更改为如下所示。您正在做的是使用 if 语句来决定变量将包含哪个查询$sql

然后,在所有 if 语句之后,您可以发送$sql查询,如上所示。如果您的查询中有错误,该if($result === false)部分会将 mysql 的投诉输出到您的屏幕上,这将有助于您调试查询。

于 2013-08-09T03:25:58.897 回答
0

您的查询一开始就不正确。一个语句中不能有两个 WHERE。以下是您的查询语句之一

SELECT tbl_books.ISBN, tbl_books.Title, 
       tbl_books.Author, tbl_books.Book_Image, 
       tbl_book_type.Name, tbl_categories.Name 
FROM tbl_books, tbl_book_type, tbl_categories
WHERE (tbl_books.Type_id = tbl_book_type.Type_id) AND 
      (tbl_books.Category_id = tbl_categories.Category_id)
WHERE tbl_books.Title LIKE '%$Search%'

你看你用WHERE了两次吗?


此外,您的代码不安全。您应该使用 PDO 或至少使用mysql_real_escape_string.

于 2013-08-09T03:21:59.290 回答