search
HomeBackend DevelopmentPHP ProblemHow 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:


                    

In the form, we need to pay attention to the following points:

  • action attribute submits the form to update.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 and name attribute. The id attribute can be used to modify the style of the form element or related JavaScript operations, and the name 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[&#39;id&#39;];
$name = $_POST[&#39;name&#39;];
$email = $_POST[&#39;email&#39;];

// 更新查询
$sql = "UPDATE MyGuests SET name=&#39;$name&#39;, email=&#39;$email&#39; 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

mysqli_real_escape_string()

. 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: <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> Summary: This article describes how to modify the database content form using PHP. In this process, we first learned how to use the

mysqli_connect()

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

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

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use