Wednesday, October 11, 2017

PHP Update Data Into MySQL Database


In this article, we are going to learn how to update records in database table using PHP Script. To update records in database table we are using PHP mysqli_query() function. This function is used to perform query operation in MySQL database.
Similarly, we have used this function to perform insertion operation in database.

NOTE : mysql_query() function is deprecated, since PHP 5.5 Version.
              mysqli_connect() function is not available form PHP 7.

Here we are updating the salary of below employee :

PHP MySQLi Update Record Example

<?php  
$host = 'localhost';  // set the hostname
$user = '';  // set the mysql username
$pass = '';  // set the mysql passowrd
$dbname = 'companyrecord';  
// connecting to mysql database  
$conn = mysqli_connect($host, $user, $pass,$dbname);  
if(!$conn){  
  die('Could not connect: '.mysqli_connect_error());  
}  
echo 'Connected to database successfully<br/>';  
  
$id=2;  
$empname="skptricks";  
$empsalary=230000;  
$sqlquery = "update employee set name=\"$name\", salary=$salary where id=$id";  
// performing the query operation
if(mysqli_query($conn, $sqlquery)){  
 echo "Record updated to database successfully";  
}else{  
echo "Could not update record: ". mysqli_error($conn);  
}  
  
mysqli_close($conn);  
?>

Output:
-------------------------
Connected to database successfully
Record updated to database successfully


No comments:

Post a Comment