HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

PHP MySQL Update Data

The UPDATE statement is used to update existing records in a table:

Select and Order Data From a MySQL Database

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value 

you can use either MySQLi or PDO. Below are examples of how you can do it:

Using MySQLi:

<?php

// MySQLi Configuration
$servername = "localhost"; // Change this to your MySQL server address
$username = "username"; // Change this to your MySQL username
$password = "password"; // Change this to your MySQL password
$database = "dbname"; // Change this to your MySQL database name

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Update condition
$user_id = 1; // Example condition

// New data
$new_name = "John Doe Updated";
$new_email = "john_updated@example.com";

// SQL statement for update
$sql = "UPDATE users SET name = ?, email = ? WHERE id = ?";

// Prepare the statement
$stmt = $conn->prepare($sql);

// Bind parameters to placeholders
$stmt->bind_param("ssi", $new_name, $new_email, $user_id); // "ssi" represents string, string, and integer data types

// Execute the statement
if ($stmt->execute()) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

// Close statement and connection
$stmt->close();
$conn->close();
?>
You can click on above box to edit the code and run again.

Output

Using PDO :

<?php

// PDO Configuration
$servername = "localhost"; // Change this to your MySQL server address
$username = "username"; // Change this to your MySQL username
$password = "password"; // Change this to your MySQL password
$database = "dbname"; // Change this to your MySQL database name

try {
    // Connect to MySQL using PDO
    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
    
    // Set PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Update condition
    $user_id = 1; // Example condition
    
    // New data
    $new_name = "John Doe Updated";
    $new_email = "john_updated@example.com";
    
    // SQL statement for update
    $sql = "UPDATE users SET name = :name, email = :email WHERE id = :id";
    
    // Prepare the statement
    $stmt = $conn->prepare($sql);
    
    // Bind values to named placeholders
    $stmt->bindParam(':name', $new_name);
    $stmt->bindParam(':email', $new_email);
    $stmt->bindParam(':id', $user_id);
    
    // Execute the statement
    if ($stmt->execute()) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record";
    }
} catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}

// Close connection
$conn = null;
?>
You can click on above box to edit the code and run again.

Output



In both examples:
Both examples will update a record in the users table where the id column matches a specified condition (in this case, $user_id). Adjust the condition and column names as needed for your use case.