Home Backend Development PHP Problem How to modify data in the database through PHP code

How to modify data in the database through PHP code

Apr 06, 2023 am 09:15 AM

When developing web applications using PHP, the database is an important part. In most cases, we need to add, delete or modify data in the database. In this article, we will discuss how to modify data in the database through PHP code.

The following is a sample database table:

CREATE TABLE users (
  id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  firstname VARCHAR(30) NOT NULL,
  lastname VARCHAR(30) NOT NULL,
  email VARCHAR(50),
  reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
Copy after login

Now, we have a user whose email address needs to be updated. We can use the following PHP code to achieve this:

// 创建数据库连接
$conn = mysqli_connect("localhost", "username", "password", "myDB");

// 检查连接是否成功
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// 为用户设置新的邮箱地址
$sql = "UPDATE users SET email='newemail@example.com' WHERE id=1";

// 执行查询
if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

// 关闭连接
$conn->close();
Copy after login

The above code first creates a MySQL database connection. After the connection is successful, we store the SQL statement UPDATE users SET email='newemail@example.com' WHERE id=1 in a variable. This statement will update the email address of the record with ID in the users table. Next, we execute the SQL statement through the query() function. If the query is successful, Record updated successfully will be output. If the query fails, an error message will be printed.

In actual applications, you may need to pass data from the form to the PHP script. Here is an example that implements this functionality:

<form action="update.php" method="post">
  <label for="firstname">First Name:</label>
  <input type="text" id="firstname" name="firstname"><br><br>
  <label for="lastname">Last Name:</label>
  <input type="text" id="lastname" name="lastname"><br><br>
  <label for="email">Email:</label>
  <input type="text" id="email" name="email"><br><br>
  <input type="submit" value="Submit">
</form>
Copy after login

In the above code, when the user submits the form, the form data will be sent to a PHP script named update.php via the POST method middle. This script will receive the form data and use it to update records in the database. Here is the PHP code to achieve this:

// 创建数据库连接
$conn = mysqli_connect("localhost", "username", "password", "myDB");

// 检查连接是否成功
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// 获取表单数据
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];

// 更新数据库记录
$sql = "UPDATE users SET firstname='$firstname', lastname='$lastname', email='$email' WHERE id=1";

// 执行查询
if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

// 关闭连接
$conn->close();
Copy after login

In the above code, we first get the data submitted through the form and store it in a variable. Next, we insert data into the database using an update statement. Finally, we check whether the record was successfully updated by querying the execution results.

Summary: Modifying data in the database through PHP code is an essential step in web development. We can achieve this using some basic SQL statements and PHP functions. This article provides some examples to help you understand how to apply these techniques in real-world applications.

The above is the detailed content of How to modify data in the database through PHP code. For more information, please follow other related articles on the PHP Chinese website!

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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24