您好,我需要一些帮助。我目前正在尝试创建一个搜索页面,用户可以通过书籍标题,类别,年份和出版商搜索特定图书
我已经创建了我的基本HTML搜索表单,这是以下代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Page Title</title>
</head>
<body>
<form id ="SearchPage" action="SearchPage.php" method="get">
<h1> Search Page</h1>
Book Title <input type="text" name="bookTitle" />
Category <select name="catDesc">
<option value = "Business & Commerce">Business & Commerce</option>
<option value = "Databases">Databases</option>
<option value = "Databases and Web Development">Databases and Web Development</option>
<option value = "Fiction">Fiction</option>
<option value = "Flex & Flash Programming">Flex & Flash Programming</option>
<option value = "Netorks">Netorks</option>
<option value = "Programming">Programming</option>
<option value = "Systems Design">Systems Design</option>
<option value = "Web Development">Web Development</option>
</select>
Publisher <input type="text" name="pubName"/>
Year <input type="text" name="bookYear"/>
<input type ="submit" value-"Find Books"/>
</form>
</body>
</html>
这是我在下面做的PHP代码:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Search Page</title>
</head>
<body>
<table>
<tr>
<th>Book Title</th>
<th>Year</th>
<th>Category</th>
<th>Publisher</th>
</tr>
<?php
include 'database_mysqli_conn.php';
$bookTitle = $_REQUEST['bookTitle'];
$bookYear =$_REQUEST['bookYear'];
$catDesc =$_REQUEST['catDesc'];
$pubName =$_REQUEST['pubName'];
$sql = "SELECT bookTitle, bookYear, catDesc, pubName, FROM nbc_book b inner join nbc_category c on b.catID = c.catID inner join nbc_publisher p on b.pubID = p.pubID WHERE 1";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $bookTitle, $bookYear, $bookPrice, $catDesc);
if (!empty($bookTitle)) {
$sql= $sql." AND bookTitle = '$bookTitle'";
}
if (!empty($bookYear)) {
$sql= $sql." AND bookYear = '$bookYear'";
}
if (!empty($catDesc)) {
$sql= $sql." AND catDesc = '$catDesc'";
}
if (!empty($pubName)) {
$sql= $sql." AND pubName = '$pubName'";
}
while (mysqli_stmt_fetch($stmt)){
echo "<tr>
<td>$bookTitle</td>
<td>$bookYear</td>
<td>$catDesc</td>
<td>$pubName</td>
</tr>";
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
?>
</body>
</html>
问题是,这是我提交搜索时收到的错误消息:
警告:mysqli_stmt_execute()要求参数1为mysqli_stmt,第27行/home/unn_w11036829/public_html/PHPexercises/SearchPage.php中给出布尔值
警告:mysqli_stmt_bind_result()要求参数1为mysqli_stmt,布尔值在第28行的/home/unn_w11036829/public_html/PHPexercises/SearchPage.php中给出
警告:mysqli_stmt_fetch()要求参数1为mysqli_stmt,第54行/home/unn_w11036829/public_html/PHPexercises/SearchPage.php中给出布尔值
警告:mysqli_stmt_close()要求参数1为mysqli_stmt,第63行/home/unn_w11036829/public_html/PHPexercises/SearchPage.php中给出布尔值 书名年份类别出版商
任何人都可以告诉我哪里出错了!过去一小时我一直坚持这一点,它让我疯了。
提前谢谢
答案 0 :(得分:2)
您的查询准备失败,您未能检查失败等等......
$sql = "SELECT bookTitle, bookYear, catDesc, pubName, FROM nbc_book b inner join ..."
^--- stray comma
从不 EVER 假设数据库查询成功。特别是在发展中。总是假设失败,检查失败,并将成功视为一个惊喜:
$stmt = mysqli_prepare($conn, $sql) or die(mysqli_error($conn));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
答案 1 :(得分:0)
CHANGE
$sql = "SELECT bookTitle, bookYear, catDesc, pubName, FROM nbc_book b inner join nbc_category c on b.catID = c.catID inner join nbc_publisher p on b.pubID = p.pubID WHERE 1";
要
$sql = "SELECT bookTitle, bookYear, catDesc, pubName FROM nbc_book b inner join nbc_category c on b.catID = c.catID inner join nbc_publisher p on b.pubID = p.pubID WHERE 1";
注意pubName之后的逗号。
编辑:你应该包装你的mysqli_stmt_execute($ stmt)在if中确保你正在捕捉这样的错误
if(mysqli_stmt_execute($stmt)){
//YOUR CODE
}
else{
echo mysqli_stmt_error;
}