Home > Article > Backend Development > How to modify data in the database through PHP code
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 );
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();
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>
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();
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!