The first is to query all, the steps do not detail, connect the database, query all the information in the table, the Foreach loop is printed in tabular form
Then is the form form to submit the data of the query, which is submitted to this page by post, so to determine whether there is a value in post, in determining whether the value of the post is an empty string (both cases the default user is not input, Query all) Default rules: Generally do query when: The user does not enter any content is to query all
Then the most important thing is the stitching query condition
Key code:
$TJ 1 = "1=1"; $TJ 2 = "1=1"; $name = ""; if (!empty ($_post)) {if ($_post["name"]! = ") {$name = $_post[" name "]; $TJ 1 = "name like '%{$name}% '"; if ($_post["brand"]! = ") {$TJ 2 =" brand = ' {$_post[' brand '} ' "; }} $TJ = "where". $tj 1. "and". $TJ 2; $sql = "SELECT * from car". $tj;
Complete Example:<! doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "HTTP// Www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Multi-conditional query in PHP