How to implement product query function in php

尊渡假赌尊渡假赌尊渡假赌
Release: 2023-06-06 16:46:43
Original
1574 people have browsed it

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.

How to implement product query function in php

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>
Copy after login

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[&#39;name&#39;];
$category = $_POST[&#39;category&#39;];
$is_on_sale = $_POST[&#39;is_on_sale&#39;];
$where = " WHERE 1 = 1";
if($name) {
    $where .= " AND name LIKE &#39;%$name%&#39;";
}
if($category) {
    $where .= " AND category = $category";
}
if($is_on_sale) {
    $where .= " AND is_on_sale = 1";
}
$sql = "SELECT * FROM goods $where";
Copy after login

3. Use PHP Connect to the database, execute SQL query statements, and obtain product data that meets the conditions:

$mysqli = new mysqli(&#39;localhost&#39;, &#39;root&#39;, &#39;123456&#39;, &#39;shop&#39;);
$result = $mysqli->query($sql);
Copy after login

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 = &#39;&#39;;while($row = $result->fetch_assoc()) {
$html .= &#39;<div>&#39;;
$html .= &#39;<p>&#39;.$row[&#39;name&#39;].&#39;</p>&#39;;
$html .= &#39;<p>&#39;.$row[&#39;price&#39;].&#39;</p>&#39;;
$html .= &#39;</div>&#39;;}echo $html;
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template