<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
语句并发布了函数。我的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'");
}
我现在需要把它提升到一个新的水平并想知道:
如果用户输入两个或三个字段而不仅仅是一个字段(例如作者和价格或作者和标题),如何检索表记录
靠近&#39;价格&#39;文本字段我需要添加两个单选按钮,允许用户检查的价格低于或高于他在文本框中输入的价格
答案 0 :(得分:0)
// 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);
}
//它会起作用