How to modify database content form in PHP
As the Internet continues to develop, websites and applications are becoming more and more common. PHP is a commonly used web development language that can be used to create interactive web applications. PHP is related to data storage, so knowing how to use PHP to operate databases is an important part of web development. This article will introduce how to modify the database content form in PHP.
First, let us understand how to connect to the database. In PHP, you can use the built-in function mysqli_connect() to connect to a MySQL database. The following is a simple connection example code:
$servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // 创建连接 $conn = mysqli_connect($servername, $username, $password, $dbname); // 检查连接是否成功 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully";
The above code will create a MySQL connection object named $conn
. If the connection fails, the die()
function will be used to output an error message.
Next, we need to prepare a table to modify the database content. The table contains the items that need to be updated, the corresponding database fields, and the initial values. Here is a basic example of modifying the form:
<form action="update.php" method="post"> <label for="id">ID:</label> <input type="text" id="id" name="id" value=""> <label for="name">Name:</label> <input type="text" id="name" name="name" value=""> <label for="email">Email:</label> <input type="text" id="email" name="email" value=""> <input type="submit" value="Update"> </form>
In the form, we need to pay attention to the following points:
-
action
attribute submits the form toupdate.php
file to handle update operations. -
method
The attribute sets the type of HTTP request. Here we use the POST method. - Each form element contains an
id
andname
attribute. Theid
attribute can be used to modify the style of the form element or related JavaScript operations, and thename
attribute will be used to interact with PHP when submitting the form. - Every element in the form has a default value, which makes it easier for users to modify it.
Now that we have the methods and forms to connect to the database, next, we will show how to update the database content.
In the update.php
file we will handle the update operation. First, we need to get the values from the form and use them to update the database. The following is a sample code for an update operation:
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // 创建连接 $conn = mysqli_connect($servername, $username, $password, $dbname); // 检查连接是否成功 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } // 获取表单提交的值 $id = $_POST['id']; $name = $_POST['name']; $email = $_POST['email']; // 更新查询 $sql = "UPDATE MyGuests SET name='$name', email='$email' WHERE id=$id"; if (mysqli_query($conn, $sql)) { echo "Record updated successfully"; } else { echo "Error updating record: " . mysqli_error($conn); } mysqli_close($conn); ?>
The above code will take the value submitted in the form and use it to update the relevant database record. In this example, the table we are updating is named MyGuests
, which will update the corresponding name (i.e. $name
) and email (i.e. $email
). If the update operation is successful, "Record updated successfully" will be output. If the update operation fails, an error message will be printed. Finally, one thing we need to pay attention to is protecting the code from SQL injection attacks. This can be achieved by using PHP's built-in function
. This function will ensure that the input value does not contain malicious content in the MySQL statement. The following is a modified example code that uses the mysqli_real_escape_string()
function to protect the input value: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">$id = mysqli_real_escape_string($conn, $_POST['id']);
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);</pre><div class="contentsignin">Copy after login</div></div>
Summary: This article describes how to modify the database content form using PHP. In this process, we first learned how to use the
function to connect to a MySQL database, and referred to a basic example of modifying a table and sample code for update operations. Finally, we secure the code against SQL injection attacks by protecting the input values using the mysqli_real_escape_string()
function.
The above is the detailed content of How to modify database content form in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics









