Skip to content

Comprehensive Guide to Updating Data in a MySQL Database using PHP

Updating data in a database is an essential operation for web applications that deal with dynamic data. This article will provide you with a step-by-step guide to update data in a MySQL database using PHP.

Understanding the PHP MySQL Update Syntax

The basic syntax of updating data in a MySQL database using PHP is as follows:

Basic syntax of updating data in a MySQL database using PHP

php
$sql = "UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE some_column = some_value";

In this syntax, table_name is the name of the table that you want to update, column1 and column2 are the names of the columns that you want to change, value1 and value2 are the new values that you want to assign to the columns, and some_column and some_value are the conditions for updating the data.

Connecting to a MySQL Database using PHP

Before updating data in a MySQL database, you need to connect to the database using PHP. The following code demonstrates how to connect to a MySQL database using PHP:

Connect to a MySQL database using PHP

php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

In this code, $servername, $username, $password, and $dbname are the details of your MySQL database, and mysqli_connect() is the function used to connect to the database.

Updating Data in a MySQL Database using PHP

Once you have connected to the database, you can start updating data in the database using PHP. In practice, connection code is typically extracted to a reusable file or function to avoid duplication. The following code demonstrates how to update data in a MySQL database using prepared statements to prevent SQL injection:

Update data in a MySQL database using PHP

php
<?php
// In practice, use require 'db_connect.php'; here to avoid duplication
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Use prepared statements to prevent SQL injection
$sql = "UPDATE table_name SET column1 = ?, column2 = ? WHERE some_column = ?";
$stmt = mysqli_prepare($conn, $sql);

// Set values first
$value1 = "new_value1";
$value2 = "new_value2";
$some_value = 1;

// Bind parameters (i = integer, s = string, d = double, b = blob)
// Ensure the type string matches the actual data types of your variables
mysqli_stmt_bind_param($stmt, "ssi", $value1, $value2, $some_value);

// Execute the statement
if (mysqli_stmt_execute($stmt)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_stmt_error($stmt);
}

mysqli_stmt_close($stmt);
mysqli_close($conn);
?>

In this code, mysqli_prepare() creates a prepared statement, and mysqli_stmt_bind_param() safely binds variables to the placeholders. This prevents SQL injection attacks. If the update was successful, mysqli_stmt_execute() returns true, and the message "Record updated successfully" will be displayed. If the update was not successful, it returns false, and the error message will be displayed.

Note: Always ensure the type specifier string matches the actual data types of your variables to prevent silent conversion issues. For modern PHP applications, consider using PDO with prepared statements as an alternative to mysqli. PDO offers a consistent interface across multiple database systems.

Practice

What is important to note when updating data with PHP in MySQL?

Dual-run preview — compare with live Symfony routes.