【发布时间】:2019-04-24 16:47:21
【问题描述】:
我有一个表格,它显示来自 mysql 数据库的图书列表和用户可以搜索图书的搜索表单。 当用户通过标题和作者的输入值进行搜索时,我期待按标题和作者显示图书列表,并在没有任何记录时将“NO Books by name or author”显示为回显。
我的代码是
<!DOCTYPE HTML>
<html>
<body bgcolor="87ceeb">
<center><h2>Central Department of Physics</h2></center>
<br>
<?php
include("DBConnection.php");
$search = isset($_REQUEST["search"]) ? $_REQUEST["search"] : '';
$query = "select ISBN,Title,Author,Edition,Publication from book_info where author like '%$search%' or title like '%$search%'";
//search with a book name in the table book_info
$result = mysqli_query($db,$query);
?>
<a href="searchform.php" class="btn btn-primary">Go Back</a>
<table border="2" align="center" cellpadding="5" cellspacing="5">
<tr>
<th> ISBN </th>
<th> Title </th>
<th> Author </th>
<th> Edition </th>
<th> Publication </th>
</tr>
<?php
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row["ISBN"];?> </td>
<td><?php echo $row["Title"];?> </td>
<td><?php echo $row["Author"];?> </td>
<td><?php echo $row["Edition"];?> </td>
<td><a href="<?php echo $row["Publication"];?>" target="_blank">Click Me</a></td>
</tr>
<?php
}
}
else{ ?>
<tr>
<td colspan="5">
<center>No books found in the library by the name $search </center>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>
<br>
我的搜索表单是
<!DOCTYPE HTML>
<html>
<body bgcolor="87ceeb">
<form action = "DisplayBooks.php" method="get">
<br>
<center>Enter the title of the book to be searched :
<input type="text" name="search" size="48">
<br></br>
<input type="submit" value="submit">
<input type="reset" value="Reset">
</center>
<br>
</form>
</body>
</html>
但是它成功地显示了书籍列表但是当没有任何记录时..它不启动回显。
ps。如何添加链接按钮,使其显示返回搜索结果以将用户导航到 Searchform,并且用户可以返回上一个表单。
【问题讨论】:
-
这对 SQL 注入开放。如果您重视为此付出的时间和精力,请使用准备好的陈述。我们不希望您的数据库遭到破坏并可能被删除。
-
@FunkFortyNiner 不,这只是测试,没关系....由于 Delwar,回声问题已得到解决,但我想添加按钮以从结果返回 Serchform....只有我没有得到。