Update multiple columns in MySQL using PHP

A guide on how to update multiple columns in MySQL using PHP. This PHP code uses the UPDATE statement to change multiple column values in a MySQL database.

<?php
// Connect to MySQL
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database_name";

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

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

// SQL query to update multiple columns
$sql = "UPDATE table_name SET column1='new_value1', column2='new_value2', column3='new_value3' WHERE id=1";

// Execute the query
if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

// Close connection
$conn->close();
?>

Detailed explanation:

  1. Connecting to MySQL:

    • We use the mysqli object to connect to the MySQL database, supplying server, username, password, and database name.
  2. UPDATE with multiple columns:

    • The SQL query UPDATE table_name SET column1='new_value1', column2='new_value2', column3='new_value3' WHERE id=1 updates the values of three columns column1, column2, and column3 for the row with id=1.
  3. Executing and checking the query:

    • We use $conn->query($sql) to execute the query. If successful, it prints "Record updated successfully", otherwise, it outputs the error.
  4. Closing the connection:

    • After completing the operation, we use $conn->close() to close the connection to MySQL.

PHP Version:

This script is compatible with PHP versions 5.6 and above. The required functions such as mysqli and strtr are supported across these versions.



Related

Multithreading in PHP using the pthreads library

A comprehensive guide on how to implement multithreading in PHP using the `pthreads` library. This article covers installation steps, examples, and how to use multithreading to improve PHP application performance.
Convert accented Unicode characters to non-accented in PHP

A guide on how to convert accented Unicode characters in the Vietnamese alphabet to non-accented letters using PHP. This PHP code efficiently handles Vietnamese text processing.
Creating a Simple Chat Application Using Socket.IO in PHP

A step-by-step guide to creating a simple chat application using Socket.IO in PHP, helping you understand how to utilize WebSocket for real-time communication.
Comprehensive guide to concatenating strings in PHP

A detailed guide on all the ways to concatenate strings in PHP, including the concatenation operator, string functions, and other methods.
How to extract numbers from a string in PHP

This article demonstrates how to extract numbers from a string in PHP using various methods such as regex (regular expressions), built-in functions like `preg_match_all`, `filter_var`, and manual approaches.
How to use the str_pad() function in PHP

A detailed guide on how to use the `str_pad()` function in PHP to pad strings with different characters to a specified length. The article introduces various common uses of this function in programming.
Example of Factory Pattern in PHP

A guide on the Factory Pattern in PHP with a concrete example, explaining how to implement and use this design pattern in object-oriented programming.
How to use strtok() function in PHP

This article explains how to use the `strtok` function in PHP to split a string into smaller parts based on delimiters. The `strtok` function is a useful tool for string manipulation in PHP projects.
How to check for a substring in a string in PHP

A guide on how to use the `strpos` function in PHP to check if a substring exists within a larger string. This is a simple and efficient way to handle string operations in PHP.
Example of Object-Oriented Programming (OOP) in PHP

A guide with a basic example of Object-Oriented Programming (OOP) in PHP, explaining how to use classes and objects to structure code using OOP principles.

main.add_cart_success