Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Mysql Update Data in PHP

Mysql Update Data in PHP

To update data in a MySQL database using MySQLi in PHP, you can follow these steps:

  1. Connect to the MySQL database.

  2. Prepare and execute the SQL UPDATE query.

  3. Check if the update was successful.

Example: Update Data Using MySQLi (Object-Oriented)

<?php// Database connection parameters$host = "localhost";$username = "root";$password = "";$dbname = "testDB"; // Your database name// Create connection$conn = new mysqli($host, $username, $password, $dbname);// Check connectionif ($conn->connect_error) {    die("Connection failed: " . $conn->connect_error);}// SQL query to update data$sql = "UPDATE users SET name = ?, email = ? WHERE id = ?";// Prepare the statement$stmt = $conn->prepare($sql);// Bind the parameters (s = string, i = integer)$stmt->bind_param("ssi", $name, $email, $id);// Set the values for the parameters$name = "Jane Doe";  // New name$email = "jane.doe@example.com";  // New email$id = 1;  // ID of the record to update// Execute the query$stmt->execute();// Check if the update was successfulif ($stmt->affected_rows > 0) {    echo "Record updated successfully.";} else {    echo "No records were updated or an error occurred.";}// Close the statement and connection$stmt->close();$conn->close();?>

Explanation:

  • $conn->prepare($sql): Prepares the SQL query with placeholders (?) for the values to be updated.

  • $stmt->bind_param("ssi", $name, $email, $id): Binds the variables to the prepared statement. The "ssi" parameter tells MySQLi that the first two values are strings (s) and the last one is an integer (i).

  • $stmt->execute(): Executes the prepared statement.

  • $stmt->affected_rows: Checks how many rows were affected by the update query. If it’s greater than 0, the update was successful.

Example: Update Data Using MySQLi (Procedural)

<?php// Database connection parameters$host = "localhost";$username = "root";$password = "";$dbname = "testDB"; // Your database name// Create connection$conn = mysqli_connect($host, $username, $password, $dbname);// Check connectionif (!$conn) {    die("Connection failed: " . mysqli_connect_error());}// SQL query to update data$sql = "UPDATE users SET name = ?, email = ? WHERE id = ?";// Prepare the statement$stmt = mysqli_prepare($conn, $sql);// Bind the parameters (s = string, i = integer)mysqli_stmt_bind_param($stmt, "ssi", $name, $email, $id);// Set the values for the parameters$name = "Jane Doe";  // New name$email = "jane.doe@example.com";  // New email$id = 1;  // ID of the record to update// Execute the querymysqli_stmt_execute($stmt);// Check if the update was successfulif (mysqli_stmt_affected_rows($stmt) > 0) {    echo "Record updated successfully.";} else {    echo "No records were updated or an error occurred.";}// Close the statement and connectionmysqli_stmt_close($stmt);mysqli_close($conn);?>

Explanation:

  • mysqli_prepare($conn, $sql): Prepares the SQL query for execution.

  • mysqli_stmt_bind_param($stmt, "ssi", $name, $email, $id): Binds the parameters (name, email, id) to the prepared statement. The "ssi" means two strings and one integer.

  • mysqli_stmt_execute($stmt): Executes the prepared statement.

  • mysqli_stmt_affected_rows($stmt): Checks how many rows were affected by the update. If it's greater than 0, the update was successful.

Key Points:

  1. Prepared Statements: Using prepared statements ensures that user input is properly escaped, preventing SQL injection vulnerabilities.

  2. Check Affected Rows: After executing the update, you can check how many rows were affected using affected_rows. If it's greater than 0, the update was successful.

  3. Bind Parameters: Always bind user input parameters to prevent SQL injection.

Let me know if you need further help with MySQLi updates or any other PHP-related topics!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql