- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP exit()
- PHP exit() vs. break
- PHP isset()
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- PHP mysqli: Insert Record
- PHP mysqli: Update Record
- PHP mysqli: Fetch Record
- PHP mysqli: Delete Record
- PHP mysqli: SignUp Page
- PHP mysqli: LogIn Page
- PHP mysqli: Store User Data
- PHP mysqli Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP mysqli code to update data
This article is created to describe the way to update or modify specific data using PHP mysqli script. These are the two approaches, that can be used to update any specific or all data in the database:
- Using PHP mysqli object-oriented script
- Using PHP mysqli procedural script
Whatever the approach we choose to update the data, we need to follow these simple steps:
- Open a connection to the database.
- Write an SQL statement regarding the data modification.
- Initialize the written SQL statement to a variable.
- Use this variable to perform the query against the database to modify particular data.
- Close the database connection.
The SQL statement to update specific data is:
UPDATE tableName SET column1=value1, column2=value2, column3=value3, ..., columnN=valueN WHERE particularColumn=particularValue;
Be careful when updating the data. The WHERE clause is used when we need to update particular records.
Be careful when omitting the WHERE clause; update all records in the table.
Update data using PHP mysqli Object-Oriented Script
Follow the example given below to update particular data (a record or row) using a PHP mysqli object-oriented script. In this example, I am going to update the age of a record whose id is 3.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "codescracker";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_errno)
{
echo "Connection to the database failed!<BR>";
echo "Reason: ", $conn->connect_error;
exit();
}
$sql = "UPDATE customer SET age='40' WHERE id=3";
$result = $conn->query($sql);
if($result)
{
echo "Data updated successfully.";
// block of code to process further...
}
else
{
echo "Error occurred while updating the record!<BR>";
echo "Reason: ", $conn->error;
}
$conn->close();
?>
Before executing the above PHP script, here is a snapshot of the table customer:
Here is the output produced by the above PHP example on updating the record using a PHP mysqli object-oriented script:
And following is the snapshot of the table named "student" after executing the above PHP script:
Note: The mysqli() function is used to open a connection to the MySQL database server in object-oriented style.
Note: The new keyword is used to create a new object.
Note: The connect_errno is used to get or return the error code (if any) from the last connect call in object-oriented style.
Note: The connect_error is used to get the error description (if any) from the last connection in object-oriented style.
Note: The exit() function is used to terminate the execution of the current PHP script.
Note: The query() function is used to perform queries on the MySQL database in object-oriented style.
Note: The error is used to return the description of the error (if any) from the most recent function call in object-oriented style.
Note: The close() function is used to close an opened connection in object-oriented style.
Note: If you remove the WHERE clause from the above PHP script, then theĀ age column of all rows will be set to 40.
The above example can also be written as:
<?php $conn = new mysqli("localhost", "root", "", "codescracker"); if(!$conn->connect_errno) { if($conn->query("UPDATE customer SET age='40' WHERE id=3")) echo "Data updated successfully."; } $conn->close(); ?>
Update data using PHP mysqli procedural script
To update data using a PHP mysqli procedural script, follow the example given below:
<?php $conn = mysqli_connect("localhost", "root", "", "codescracker"); if(!mysqli_connect_errno()) { $sql = "UPDATE customer SET age='42' WHERE id=3"; if(mysqli_query($conn, $sql)) echo "Data updated successfully."; else { echo "Error occurred while updating the record!<BR>"; echo "Reason: ", mysqli_error($conn); } } mysqli_close($conn); ?>
Note: The mysqli_connect() function is used to open a connection to the MySQL database server in procedural style.
Note: The mysqli_connect_errno() function is used to get or return the error code (if any) from the last connect call in procedural style.
Note: The mysqli_query() function is used to perform queries on the MySQL database in procedural style.
Note: The mysqli_error() function is used to return the description of the error (if any) from the most recent function call in procedural style.
Note: The mysqli_close() function is used to close an opened connection to the MySQL database in procedural style.
PHP mysqli Object-Oriented: Update Multiple Columns
To update multiple columns at once, everything will be the same as done in the section Update Data using PHP mysqli Object-Oriented Script, except the SQL statement. That is, to update two columns, say age and email, use the following SQL statement:
$sql = "UPDATE customer SET age='42', email='newmail@xyz.com' WHERE id=3";
And to update more columns, say three columns, use the following SQL statement:
$sql = "UPDATE customer SET name='Lucas', age='42', email='newmail@xyz.com' WHERE id=3";
Here is the complete PHP mysqli script to update multiple columns at once:
<?php $conn = new mysqli("localhost", "root", "", "codescracker"); if(!$conn->connect_errno) { $sql = "UPDATE customer SET name='Lucas', age='42', email='newmail@xyz.com' WHERE id=3"; if($conn->query($sql)) echo "Data updated successfully."; else { echo "Error occurred while updating the record!<BR>"; echo "Reason: ", $conn->error; } } $conn->close(); ?>
PHP mysqli Procedural: Update Multiple Columns
<?php $conn = mysqli_connect("localhost", "root", "", "codescracker"); if(!mysqli_connect_errno()) { $sql = "UPDATE customer SET name='Lucas', age='42', email='newmail@xyz.com' WHERE id=3"; if(mysqli_query($conn, $sql)) echo "Data updated successfully."; else { echo "Error occurred while updating the record!<BR>"; echo "Reason: ", mysqli_error($conn); } } mysqli_close($conn); ?>
PHP mysqli: Update All Rows at Once
To update all rows using the PHP mysqli script, all the processes will be the same, except that you will remove or omit the WHERE clause. That is, to update all rows with particular columns, use this similar SQL statement:
$sql = "UPDATE customer SET email='newmail@xyz.com'";
And to update all rows with multiple columns, use this similar SQL statement:
$sql = "UPDATE customer SET name='Lucas', age='42', email='newmail@xyz.com'";
« Previous Tutorial Next Tutorial »