PHP MySQLi Update Query
The update keyword is basically used to modify or edit the existing records in the database table. it usually needs a where clause to find out in which record change is to be done. It is a must to specify the where clause otherwise all records of that table got modify.
UPDATE table_name SET column1=value, column2=value2 WHERE some_column=some_value
id | name | salary |
---|---|---|
1 | Name1 | 55000 |
2 | Name2 | 60000 |
Note: Notice the WHERE clause in the UPDATE syntax is a must otherwise all records will be updated!.
<?php
$host = "localhost";
$user = "";
$pass = "";
$dbname = "users";
$conn = mysqli_connect($host,$user,$pass,$dbname) or die(mysqli_error());
$id=1;
$name="Rahul";
$salary=80000;
$sql = "update users set name='".$name."',salary='".$salary."' where id='".$id;
//if not working then use all variable with in single quote
if(mysqli_query($conn,$sql)){
echo "Record updated successfully";
}else{
echo "Could not update record: ". mysqli_error($conn);
}
mysqli_close($conn);
?>
Record updated successfully
id | name | salary |
---|---|---|
1 | Rahul | 80000 |
2 | Name2 | 60000 |
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning