php method to implement the product query function: 1. Create a PHP sample file; 2. Create a product query form, and users can submit query conditions to the server through these input boxes; 3. Submit the query submitted by the user Conditions are obtained on the server side using PHP language and spliced into SQL query statements; 4. Use PHP to connect to the database, execute SQL query statements, and obtain product data that meets the conditions; 5. Process the queried product data using PHP language and splice it together into HTML format, return it to the user, and complete the product query function.
Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.
How to implement the product query function in php:
1. First, create a product query form. The form can have input boxes, drop-down boxes, check boxes, etc. Users can These input boxes submit query conditions to the server:
<form action="query.php" method="post"> <input type="text" name="name" placeholder="商品名称"> <select name="category"> <option value="">请选择分类</option> <option value="1">电子产品</option> <option value="2">家用电器</option> <option value="3">服装鞋帽</option> </select> <input type="checkbox" name="is_on_sale" value="1"> 仅显示上架商品 <input type="submit" value="查询"> </form>
2. Use the PHP language to obtain the query conditions submitted by the user on the server side and splice them into SQL query statements:
$name = $_POST['name']; $category = $_POST['category']; $is_on_sale = $_POST['is_on_sale']; $where = " WHERE 1 = 1"; if($name) { $where .= " AND name LIKE '%$name%'"; } if($category) { $where .= " AND category = $category"; } if($is_on_sale) { $where .= " AND is_on_sale = 1"; } $sql = "SELECT * FROM goods $where";
3. Use PHP Connect to the database, execute SQL query statements, and obtain product data that meets the conditions:
$mysqli = new mysqli('localhost', 'root', '123456', 'shop'); $result = $mysqli->query($sql);
4. Use PHP language to process the queried product data, splice it into HTML format, and return it to the user to complete the product query function:
$html = '';while($row = $result->fetch_assoc()) { $html .= '<div>'; $html .= '<p>'.$row['name'].'</p>'; $html .= '<p>'.$row['price'].'</p>'; $html .= '</div>';}echo $html;
The above is the detailed content of How to implement product query function in php. For more information, please follow other related articles on the PHP Chinese website!