PHP MySQL 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 = mysql_connect($host,$user,$pass) or die(mysql_error());
if(!$conn){
die("Could not connect:".mysql_connect_error());
}
// select database
mysql_select_db($dbname,$conn);
$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(mysql_query($sql)){
echo "Record updated successfully";
}else{
echo "Could not update record: ". mysql_error();
}
mysql_close();
?>
Record updated successfully
id | name | salary |
---|---|---|
1 | Rahul | 80000 |
2 | Name2 | 60000 |
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning