<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>
I have used an if/else if
statement and post function when user searches either by title, author or price(html form fields) for records in a mysql table. My if/else if statement that is based on user inputting one form field at a time:
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'");
}
I need to take it to the next level now and want to know:
How do I retrieve table records if the user inputs two or three fields and not just one(eg. author and price or author and title)
Near the 'price' text field I need to add two radio buttons which will allow the user to check less than or more than the price he inputs in the textbox for 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);
}
// It will work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.