<html>
<form action="search_book.php" method="post">
Title:<input type="text" name="title"><br>
Author:<input type="text" name="author"><br>
Price of Book:<input type="text" name="price"><input type="radio" name="radio" id="less"><Label for="less">Less Than</label><input type="radio" name="radio" id="more"><label for ="more">More Than</label<<br>
<input type="submit" value="Search">
</form>
</html>
当用户按标题、作者或价格(html 表单字段(搜索 mysql 表中的记录时,我使用了 if/else if
语句和 post 函数。我的 if/else if 语句基于用户一次输入一个表单字段:
if($_POST['title']!="" && $_POST['title']!=" "){
$title=$_POST['title'];
$query=mysqli_query($con,"SELECT * from book where Title='$title'");
} else if($_POST['author']!=""&& $_POST['author']!=" "){
$title=$_POST['author'];
$query=mysqli_query($con,"SELECT * from book where Author='$author'");
} else if($_POST['price']!="" && $_POST['price']!=" "){
$title=$_POST['price'];
$query=mysqli_query($con,"SELECT * from book where Price='$price'");
}
我现在需要把它提升到一个新的水平,想知道:
如果用户输入两个或三个字段而不仅仅是一个字段,我如何检索表记录(例如,作者和价格或作者和标题(
在"价格"文本字段附近,我需要添加两个单选按钮,这将允许用户检查小于或大于他在文本框中输入的价格
// Use the Below code it will work for all
$condition="";
if(isset($_POST) && !empty($_POST)) {
if(trim($_POST['title'])!="") {
$condition.="Title='".$_POST['title']."' AND ";
}
if(trim($_POST['author'])!="") {
$condition.="Author='".$_POST['author']."' AND ";
}
if(trim($_POST['price'])!="") {
$condition.="Price='".$_POST['price']."' AND ";
}
// remove last AND
if($condition!="") {
$condition=" WHERE ".substr($condition,0,-5);
}
$query=mysqli_query($con,"SELECT * from book".$condition);
}
它会工作