Database Update data using PHP

php Database Update data using PHP PHP Update Database Record

How to update the data into the mysql database using PHP ?

To update the data into the database we first need to establish the database connection. If you didn't know how to establish the database connection , you can visit this article ⇨PHP database connection

Update data into database using PHP mysqli function

For updating the existing data into the database we just need to learn the query that how to write the update query with where condition. We have to just pass the connection and update query with where condition into the mysqli_query function.

Update Query Syntax

"UPDATE table_name SET column1 = value1, column2 = value2, column3 = value3, ... WHERE condition" mysqli_query($connection , $query) Now we are going to update the user's record into the below users table.

Update Query Program

Here in the above table there exists two users Ali and Umer. ⇨ We want to update the first_name and password of Ali. ⇨ We will use the unique id 'id' in where condition. <?php $conn = mysqli_connect('localhost', 'root', '', 'myfirstdatabase'); mysqli_query($conn,"UPDATE users SET first_name='Hussain',password='Ali12345' WHERE id=1 "); ?>Note ! If you don't write the WHERE condition , then all records will updated.

UPDATE data using PHP mysqli function using Variables

<?php $conn = mysqli_connect('localhost', 'root', '', 'myfirstdatabase'); $first_name = 'Hussain'; $password = 'Ali12345'; mysqli_query($conn,"UPDATE users SET first_name='".$first_name."',password='".$password."' WHERE id=1 "); ?>
php PHP Update Database Record Chapter Next »