How to update statement in mysql using PHP?

Today, We want to share with you mysql update statement.In this post we will show you delete query in mysqli, hear for mysqli updates multiple rows from (select) we will give you demo and example for implement.In this post, we will learn about Updateing Data in MySQL Using PHP with an example.

PHP MySQL Update Statement

UPDATE MySQL command is used to modify rows in a table. also i learn to Insert into a MySQL table or update if exists.

MySQL Update Command Syntax

UPDATE `table_name` SET `column_name` = `new_value' [WHERE condition];

UPDATE table
SET column1 = expression1,
    column2 = expression2,
    ...
[WHERE conditions];

Example – Update single column

UPDATE players
SET product_name = 'Jignesh'
WHERE player_id = 5000;

This MySQL UPDATE example would update the product_name to ‘Jignesh’ in the players table where the player_id is 5000.

Read Also:  how to create sql database file in mysql?

Example – Update multiple columns

UPDATE players
SET state = 'Rajkot',
    player_rep = 32
WHERE player_id > 100;

This MySQL UPDATE statement example would update the state to ‘Rajkot’ and the player_rep to 32 where the player_id is greater than 100

Example – Update table with data from another table

UPDATE players
SET country = (SELECT country
            FROM teams
            WHERE teams.team_name = players.player_name)
WHERE player_id > 1998;

This UPDATE example would update only the players table for all records where the player_id is greater than 1998. When the team_name from the teams table matches the player_name from the players table, the country from the teams table would be copied to the country field in the players table.

Read Also:  MySQL Multicolumn UPDATE JOIN subquery

Example – Update multiple Tables

UPDATE players, teams
SET players.country = teams.country
WHERE players.player_id = teams.team_id;

This MySQL UPDATE statement example would update the country field in the players table to the country from the teams table where the player_id matches the team_id.

Updating Database Table Data

index.php

<?php
$db_connect = mysqli_connect("localhost", "root", "", "pakainfo");
 
if($db_connect === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$sql_query = "UPDATE players SET email='[email protected]' WHERE id=1";
if(mysqli_query($db_connect, $sql_query)){
    echo "Good Luck, Records were updated successfully.";
} else {
    echo "ERROR: Could not able to execute $sql_query. " . mysqli_error($db_connect);
}
 
// Close connection
mysqli_close($db_connect);
?>

I hope you get an idea about sql set variable.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.