PHP MySQL: Update Data

PHP MySQL: Update Data

PHP MySQL: Update Data

Updating data in a MySQL database using PHP involves executing an SQL UPDATE statement with the desired changes. This is often used to modify existing records based on specific conditions.


Steps to Update Data

  1. Connect to the MySQL Database
  2. Write the SQL UPDATE Query
  3. Execute the Query
  4. Check for Success
  5. Close the Connection

Syntax for SQL UPDATE

UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
  • table_name: The table containing the records you want to update.
  • SET: Specifies the columns and their new values.
  • WHERE: Specifies the condition to identify the records to be updated. Always include a WHERE clause to avoid updating all records unintentionally.

Code Example: Update Data

1. Establish a Database Connection

<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "example_db"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?>

2. Execute an SQL UPDATE Query

Update a user's email based on their ID:

<?php $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; } ?>

Using Prepared Statements for Security

Prepared statements are recommended for user inputs to prevent SQL injection.

<?php $stmt = $conn->prepare("UPDATE users SET email = ? WHERE id = ?"); $stmt->bind_param("si", $email, $id); // Set parameters and execute $email = "newemail@example.com"; $id = 1; $stmt->execute(); if ($stmt->affected_rows > 0) { echo "Record updated successfully"; } else { echo "No records updated or error occurred."; } // Close statement and connection $stmt->close(); $conn->close(); ?>

Updating Multiple Columns

To update multiple columns, separate each column assignment with a comma:

<?php $sql = "UPDATE users SET name = 'John Doe', email = 'john.doe@example.com' WHERE id = 1"; if ($conn->query($sql) === TRUE) { echo "Record updated successfully"; } else { echo "Error updating record: " . $conn->error; } ?>

Check the Number of Affected Rows

To verify how many rows were updated:

<?php $sql = "UPDATE users SET email = 'newemail@example.com' WHERE id = 1"; $conn->query($sql); if ($conn->affected_rows > 0) { echo "Rows updated: " . $conn->affected_rows; } else { echo "No rows updated."; } ?>

Complete Example

<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "example_db"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // SQL query $sql = "UPDATE users SET email = 'updatedemail@example.com' WHERE id = 2"; if ($conn->query($sql) === TRUE) { echo "Record updated successfully"; } else { echo "Error updating record: " . $conn->error; } // Close connection $conn->close(); ?>

Key Notes

  1. Always Use WHERE: All table rows will be updated without a clause.
  2. Use Prepared Statements: Prevent SQL injection using prepared statements for user inputs.
  3. Check Affected Rows: Use $conn->affected_rows or $stmt->affected_rows to verify the update's effect.
  4. Error Handling: Always handle errors to debug issues effectively.
  5. Back Up Data: Before running critical update queries, back up your database to prevent data loss from errors.

Let me know if you need additional assistance!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close