Skip to main content

PHP - Simple Update Table Row Using PDO


In this tutorial we will create a Simple Update Table Row using PDO. This code can update a data in the database server by the use of advance PDO query. The code use a PDO to update a specific data in the table row with user precaution for saving changes in the data.

We will be using PDO as a query scripting it an acronym for PHP Data Objects. It provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. This means developers can write portable code much easier.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_update_pdo, after that click Import then locate the database file inside the folder of the application then click ok.
tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn = new PDO( 'mysql:host=localhost;dbname=db_update_pdo', 'root', '');
  3. if(!$conn){
  4. die("Error: Failed to connect to database!");
  5. }
  6. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as shown below.
index.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a href="https://sourcecodester.com" class="navbar-brand">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Update Table Row Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;" />
  17. <table class="table table-bordered">
  18. <thead class="alert-info">
  19. <tr>
  20. <th>Firstname</th>
  21. <th>Lastname</th>
  22. <th>Username</th>
  23. <th>Password</th>
  24. <th>Action</th>
  25. </tr>
  26. </thead>
  27. <tbody>
  28. <?php
  29. require 'conn.php';
  30. $sql = $conn->prepare("SELECT * FROM `user` ORDER BY `lastname` ASC");
  31. $sql->execute();
  32. while($row = $sql->fetch()){
  33. ?>
  34. <tr>
  35. <td><?php echo $row['firstname']?></td>
  36. <td><?php echo $row['lastname']?></td>
  37. <td><?php echo $row['username']?></td>
  38. <td>********</td>
  39. <td><a class="btn btn-warning" href="edit.php?id=<?php echo $row['user_id']?>"><span class="glyphicon glyphicon-edit"></span> Edit</a></td>
  40. </tr>
  41. <?php
  42. }
  43. ?>
  44. </tbody>
  45. </table>
  46. </div>
  47. </body>
  48. </html>

edit.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a href="https://sourcecodester.com" class="navbar-brand">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Update Table Row Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;" />
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <?php
  20. if(ISSET($_GET['id'])){
  21. require_once 'conn.php';
  22. $id = $_GET['id'];
  23. $sql = $conn->prepare("SELECT * FROM `user` WHERE `user_id`='$id'");
  24. $sql->execute();
  25. $row = $sql->fetch();
  26. }
  27. ?>
  28. <form method="POST" action="update.php?id=<?php echo $id?>">
  29. <div class="form-group">
  30. <label>Firstname</label>
  31. <input class="form-control" type="text" value="<?php echo $row['firstname']?>" name="firstname"/>
  32. </div>
  33. <div class="form-group">
  34. <label>Lastname</label>
  35. <input class="form-control" type="text" value="<?php echo $row['lastname']?>" name="lastname"/>
  36. </div>
  37. <div class="form-group">
  38. <label>Username</label>
  39. <input class="form-control" type="text" value="<?php echo $row['username']?>" name="username"/>
  40. </div>
  41. <div class="form-group">
  42. <label>Password</label>
  43. <input class="form-control" type="password" value="<?php echo $row['password']?>" name="password"/>
  44. </div>
  45. <div class="form-group">
  46. <button class="btn btn-warning form-control" name="update">Save Changes</button>
  47. </div>
  48. </form>
  49. <?php
  50. $conn = null;
  51. ?>
  52. </div>
  53. </div>
  54.  
  55. </body>
  56.  
  57. </html>

Creating the Main Function

This code contains the main function of the application. This code will update a data when the button is clicked. To do this just kindly write these block of codes inside the text editor, then save it as update.php.

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['update'])){
  5. try{
  6. $id = $_GET['id'];
  7. $firstname = $_POST['firstname'];
  8. $lastname = $_POST['lastname'];
  9. $username = $_POST['username'];
  10. $password = $_POST['password'];
  11. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12. $sql = "UPDATE `user`SET `firstname` = '$firstname', `lastname` = '$lastname', `username` = '$username', `password` = '$password' WHERE `user_id` = '$id'";
  13. $conn->exec($sql);
  14. }catch(PDOException $e){
  15. echo $e->getMessage();
  16. }
  17.  
  18. $conn = null;
  19.  
  20. echo "<script>alert('Succesfully updated an account!')</script>";
  21. echo "<script>window.location='index.php'</script>";
  22. }
  23. ?>

There you have it we successfully created a Simple Update Table Row using PDO. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.