ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

14-MySQL Update Data

To Update the existing data in MySQL database you need to used UPDATE statement.

Syntax:

UPDATE tablename
SET column1=val1, column2=val2, column3=val3.
WHERE column=value;
Take Note! Using the UPDATE command you must not forget to include the WHERE clause, because the Where clause is used to specify record you want to modify. Without having the WHERE clause, all records in the table will be modified.
Example in Updating Data using MySQLi(Object-Oriented), MySQLi(Procedural) and PDO.

Example using MySQLi(Object-Oriented):

  1. <?php
  2. $server = "localhost";
  3. $username = "root";
  4. $password = "";
  5. $database = "mysqltutorial";
  6.  
  7. // Create connection
  8. $conn = new mysqli($server, $username, $password, $database);
  9. // Check connection
  10. if ($conn->connect_error) {
  11. die("Connection failed: " . $conn->connect_error);
  12. }
  13.  
  14. // sql to UPDATE record from table
  15. $sql = "UPDATE `tblpeople` SET  `LNAME` =  'Villan',`FNAME` =  'Joven',`ADDRESS` =  'Bacolod' WHERE  `tblpeople`.`ID` =5;";
  16.  
  17.  
  18. if ($conn->query($sql) === TRUE) {
  19.     echo "Record UPDATED successfully";
  20. } else {
  21.     echo "Error: " . $sql . "<br>" . $conn->error;
  22. }
  23.  
  24. $conn->close();
  25. ?>

Example using MySQLi(Procedural):

  1. <?php
  2.  
  3. $server = "localhost";
  4. $username = "root";
  5. $password = "";
  6. $database = "mysqltutorial";
  7.  
  8. // Create connection
  9. $conn = mysqli_connect($server, $username, $password, $database);
  10. // Check connection
  11. if (!$conn) {
  12.     die("Connection failed: " . mysqli_connect_error());
  13. }
  14.  
  15. // sql to UPDATE record from table
  16. $sql = "UPDATE `tblpeople` SET  `LNAME` =  'Villan',`FNAME` =  'Joven',`ADDRESS` =  'Bacolod' WHERE  `tblpeople`.`ID` =5;";
  17.  
  18. if (mysqli_query($conn, $sql)) {
  19.    
  20.     echo "Record UPDATED successfully";
  21. } else {
  22.     echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  23. }
  24.  
  25. mysqli_close($conn);
  26. ?>

Example using PDO:

  1. <?php
  2.  
  3. $server = "localhost";
  4. $username = "root";
  5. $password = "";
  6. $database = "mysqltutorial";
  7.  
  8. try {
  9.     $conn = new PDO("mysql:host=$server;dbname=$database", $username, $password);
  10.     // set the PDO error mode to exception
  11.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12.  
  13.        
  14. // sql to UPDATE record from table
  15. $sql = "UPDATE `tblpeople` SET  `LNAME` =  'Villan',`FNAME` =  'Joven',`ADDRESS` =  'Bacolod' WHERE  `tblpeople`.`ID` =5;";
  16.    
  17.     $conn->exec($sql);
  18.         echo "Record UPDATED successfully";
  19.     }
  20. catch(PDOException $e)
  21.     {
  22.    
  23.     echo $sql . "<br>" . $e->getMessage();
  24.     }
  25.  
  26. $conn = null;
  27. ?>
Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!