Home > Article > Backend Development > How to update data in database using PHP
PHP is a very popular server-side programming language. Its main features are that it is easy to learn and use, and it can easily interact with the database. In this article, we will discuss how to update data in a database using PHP.
Before updating the database, we need to connect to the database first. In PHP, we can use extensions such as mysqli or PDO to implement database connections.
For mysqli extension, we can use the following code to connect to the database:
<?php $servername = "localhost"; $username = "root"; $password = "password"; $dbname = "myDB"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接是否成功 if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?>
In the above code, we first define the server name, username, password and database required to connect to the database name. Then use the mysqli extension to create a connection and check if the connection is successful. If the connection is successful, "Connected successfully" will be output.
For PDO extension, we can use the following code to connect to the database:
<?php $servername = "localhost"; $username = "root"; $password = "password"; $dbname = "myDB"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // 设置 PDO 错误模式为异常 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?>
In the above code, we first define the server name, username, password and database required to connect to the database name. Then a PDO connection is created and the error mode is set to exception. If the connection is successful, "Connected successfully" will be output.
After connecting to the database, we can use PHP to update the data in the database, using either mysqli or PDO extension. The following is a sample code for updating the database using mysqli extension:
<?php // 获取表单数据 $name = $_POST['name']; $email = $_POST['email']; // 更新数据 $sql = "UPDATE myTable SET email='$email' WHERE name='$name'"; if ($conn->query($sql) === TRUE) { echo "Record updated successfully"; } else { echo "Error updating record: " . $conn->error; } ?>
In the above code, we first get the data that needs to be updated from the form and use the UPDATE statement to update the data in the database. If the update is successful, "Record updated successfully" will be output, otherwise an error message will be output.
The following is a sample code for updating the database using PDO extension:
<?php // 获取表单数据 $name = $_POST['name']; $email = $_POST['email']; // 更新数据 $sql = "UPDATE myTable SET email=:email WHERE name=:name"; $stmt = $conn->prepare($sql); $stmt->bindParam(':email', $email); $stmt->bindParam(':name', $name); $stmt->execute(); echo $stmt->rowCount() . " records UPDATED successfully"; ?>
In the above code, we use PDO's prepare() method to prepare the SQL statement, and use the bindParam() method to set parameter. Then use the execute() method to execute the SQL statement and output the number of updated records.
Summary
In this article, we discussed how to update data in a database using PHP. First, we demonstrated how to connect to the database and then use mysqli and the PDO extension to update the data. Remember that when updating your database using PHP, you should parameterize your queries and avoid using suspicious user input to ensure your application is safe and secure.
The above is the detailed content of How to update data in database using PHP. For more information, please follow other related articles on the PHP Chinese website!