Home > Article > Backend Development > PHP form batch modification data
With the continuous development of network technology, the number of visits to the website is getting larger and larger, so we often need to enter data into the website through forms. In the development of websites, it often involves the need to batch modify existing data, such as batch modification of user information. This article will introduce how to use PHP forms to modify data in batches.
1. Form creation
First you need to create a form to input data and submit it to the backend for processing. Various form elements can be created in the form through the "input" tag, including text boxes, radio buttons, check boxes, etc. For example, we can create a form containing five input boxes: "User ID", "Username", "Gender", "Age", and "Email", as shown below:
<form action="update.php" method="post"> <table> <tr> <th>用户ID</th> <th>用户名</th> <th>性别</th> <th>年龄</th> <th>邮箱</th> </tr> <tr> <td><input type="text" name="id[]"></td> <td><input type="text" name="username[]"></td> <td> <input type="radio" name="gender[]" value="1">男 <input type="radio" name="gender[]" value="0">女 </td> <td><input type="text" name="age[]"></td> <td><input type="text" name="email[]"></td> </tr> <tr> <td><input type="text" name="id[]"></td> <td><input type="text" name="username[]"></td> <td> <input type="radio" name="gender[]" value="1">男 <input type="radio" name="gender[]" value="0">女 </td> <td><input type="text" name="age[]"></td> <td><input type="text" name="email[]"></td> </tr> <!-- 更多数据行 --> </table> <input type="submit" value="提交"> </form>
We use the form element A "[]" is used in the "name" attribute to indicate that the element is an array, so that each value in multiple rows of data can be easily obtained.
In addition, we specify the processing file "update.php" for form submission in the "action" attribute. In this file we will process the submitted data and display the results to the user.
2. Data processing
In "update.php", we need to first obtain the data submitted by the form, and then process it according to the format of the data.
$id = $_POST['id']; $username = $_POST['username']; $gender = $_POST['gender']; $age = $_POST['age']; $email = $_POST['email']; $count = count($id); for ($i = 0; $i < $count; $i++) { $sql = "UPDATE users SET "; $sql .= "username='$username[$i]',"; $sql .= "gender='$gender[$i]',"; $sql .= "age='$age[$i]',"; $sql .= "email='$email[$i]' "; $sql .= "WHERE id='$id[$i]'"; // 执行SQL语句 }
In this code, we first use "$_POST" to get each data array submitted by the form. We then use the "count" function to get the number of elements in the array and iterate over the elements. For each element, we treat it as a recorded value and update it to the database using an "UPDATE" statement. The syntax of cross-line strings is used here to make the code more concise and readable.
3. Presentation of results
After processing the data, we need to feed the results back to the user so that they can confirm the operation. Here we can display the results of each operation in the table, allowing users to view and edit the modified data at the same time.
$result = mysqli_query($conn, $sql); if ($result) { echo "<br>数据修改成功!<br>"; } else { echo "<br>数据修改失败!<br>"; } $sql = "SELECT * FROM users"; $result = mysqli_query($conn, $sql); echo "<table border='1'>"; echo "<tr>"; echo "<th>ID</th>"; echo "<th>用户名</th>"; echo "<th>性别</th>"; echo "<th>年龄</th>"; echo "<th>邮箱</th>"; echo "</tr>"; while ($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>".$row['id']."</td>"; echo "<td>".$row['username']."</td>"; if ($row['gender'] == 1) { echo "<td>男</td>"; } else { echo "<td>女</td>"; } echo "<td>".$row['age']."</td>"; echo "<td>".$row['email']."</td>"; echo "</tr>"; } echo "</table>";
Here we process each row of data modifications and output the updated operation results. At the same time, we use the "SELECT" statement to obtain all the data in the database and traverse each row of data through the "while" loop and output it to the table. At the same time, we performed special processing on the gender data and converted it into an easy-to-understand text form.
4. Summary
Through the introduction of this article, we can understand the basic operation process of how to use PHP forms to modify data in batches. It should be noted that when processing form data, you need to pay attention to security issues, such as SQL injection. In addition, for each SQL operation, we need to provide feedback on the results after the operation is completed so that users can understand the results of the operation.
In actual development, issues such as data paging, verification, formatting, etc. also need to be considered, so as to make the operation of batch modification of data in the form more complete and convenient.
The above is the detailed content of PHP form batch modification data. For more information, please follow other related articles on the PHP Chinese website!