Home  >  Article  >  Backend Development  >  Detailed explanation of how to write PHP query button

Detailed explanation of how to write PHP query button

PHPz
PHPzOriginal
2023-03-29 11:31:131132browse

For PHP developers, the query button is a very important component. During the development of web applications, it is often necessary to add query buttons to quickly filter and search data. This article will introduce how to write query buttons in PHP.

1. HTML code

First, we need to define the query button in the HTML code. We can use the element to create a button as follows:

<input type="submit" name="search" value="查询">

In the above code, we define an element of type "submit" for submitting the form data. We also set the name "search" and the display text "query" for the button.

2. PHP code

1. Receive form data

When the user clicks the query button, the form will be submitted to the server, and the server needs to process the form data. To get form data we can use $_POST or $_GET superglobal variable.

if(isset($_POST['search'])){
   //获取表单数据
}

In the above code, we first check whether $_POST['search'] exists through the isset() function. If it exists, the query button has been clicked. We use if conditional statements to process form data.

2. Query the database

Generally, the function of the query button is to retrieve data from the database. In PHP, we can use MySQLi or PDO extensions to connect and query the database.

if(isset($_POST['search'])){
   //获取表单数据
   
   //连接数据库
   $conn = new mysqli($servername, $username, $password, $dbname);
   
   //查询语句
   $sql = "SELECT * FROM users WHERE name='$name'";
   
   //执行查询
   $result = $conn->query($sql);
   
   //处理查询结果
}

In the above code, we first use the mysqli_connect() function to connect to the MySQL database. Then, we define the query statement $sql and use the $conn->query() method to execute the query. Finally, we process the query results using

3. Display query results

The ultimate purpose of the query button is to display the query results. In PHP, we can use HTML and CSS to display query results. Here is a simple code example to display query results:

if(isset($_POST['search'])){
   //获取表单数据
   
   //连接数据库
   $conn = new mysqli($servername, $username, $password, $dbname);
   
   //查询语句
   $sql = "SELECT * FROM users WHERE name='$name'";
   
   //执行查询
   $result = $conn->query($sql);
   
   //处理查询结果
   if($result->num_rows > 0){
      //输出数据
      while($row = $result->fetch_assoc()) {
         echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
      }
   } else {
      echo "0 结果";
   }
   
   //关闭数据库连接
   $conn->close();
}

In the above code, we first check if the query result is empty. If it is not empty, use a while loop to output the query results.

Summary:

The query button is one of the commonly used components in web applications. In PHP, we can use HTML, CSS and PHP code to create and process query buttons. We can also use MySQLi or PDO extensions to connect and query the database. Once you are familiar with the above methods, you will be able to write your own query buttons to improve the user experience of your web application.

The above is the detailed content of Detailed explanation of how to write PHP query button. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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