我为学校项目建立了这个网站,它似乎没有检索数据。我一直在寻找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>
答案 0 :(得分: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
。
答案 1 :(得分:0)
正如invisal所说,你不能在一个陈述中有2个WHERE。
我建议更改你的if语句内容。而不是mysqli_query($con, "SELECT BLA BLA");
这样做:
$ sql =“SELECT 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的投诉输出到您的屏幕,这可以帮助您调试查询。