Mysql UPDATE

  • Update query is used to update entries in table.

Syntax

Syntax1
    UPDATE tableName SET column1='value1',column2='value2'...
  • It will update the entries of specified columns in all rows of the table.
Syntax2
UPDATE tableName SET column1='value1',column2='value2'... WHERE conditions
  • It will update the entries of specified columns in the rows for which the condition of WHERE clause is satisfied.
  • Note: Where Clause is used to specify some conditions in a query. After using WHERE clause, only those records will be retrieved which will fulfil the condition.

Example

We have following table with us-

idnamesalarycontact
1John250007777777
2Peter400009999999
  • Now we have to update data in the above table using following code:
<!DOCTYPE html>

<html>

<body>

<?php

$connect=mysqli_connect("localhost","root","","firstdb");

$query="UPDATE employee SET name='Tom',salary='50000' WHERE id='1'";

$result=mysqli_query($connect,$query);

?>

</body>

</html>

Output

After running the program, the table will appear as following:

idnamesalarycontact
1Tom500007777777
2Peter400009999999
  • Above program updates two columns 'name' and 'salary'. But the condition is that the id must be '1'. Therefore the changes will occur only in the row which contains '1' as the value of 'id' column.