Skip to main content

PHP - Simple Image Update


In this tutorial we will create a Simple Image Update using PHP. This code will launch a modal to update the user image when the user click the update button. The code use MySQLi UPDATE query to update the new user information base on the given user id. This a user-friendly program feel free to modify and use it to your system.

We will be using PHP as a scripting language that interepret in the webserver such as xamp, wamp, etc. It is widely use by modern website application to handle and protect user confidential information.

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 jquery that i used in this tutorial https://jquery.com/.

Lastly, 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_img_update, 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 = mysqli_connect("localhost", "root", "", "db_img_update");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

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 index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">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 Image Update</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-success" type="button" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add user</button>
  18. <br /><br />
  19. <table class="table table-bordered">
  20. <thead class="alert-info">
  21. <tr>
  22. <th>Photo</th>
  23. <th>Firstname</th>
  24. <th>Lastname</th>
  25. <th>Action</th>
  26. </tr>
  27. </thead>
  28. <tbody>
  29. <?php
  30. require 'conn.php';
  31. $query = mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error());
  32. while($fetch = mysqli_fetch_array($query)){
  33. ?>
  34. <tr>
  35. <td><img src="<?php echo $fetch['photo']?>" height="80" width="100"/></td>
  36. <td><?php echo $fetch['firstname']?></td>
  37. <td><?php echo $fetch['lastname']?></td>
  38. <td><button type="button" class="btn btn-warning" data-toggle="modal" data-target="#edit<?php echo $fetch['user_id']?>"><span class="glyphicon glyphicon-edit"></span> Update</button></td>
  39. <div class="modal fade" id="edit<?php echo $fetch['user_id']?>" aria-hidden="true">
  40. <div class="modal-dialog">
  41. <div class="modal-content">
  42. <form method="POST" enctype="multipart/form-data" action="edit.php">
  43. <div class="modal-header">
  44. <h3 class="modal-title">Edit User</h3>
  45. </div>
  46. <div class="modal-body">
  47. <div class="col-md-2"></div>
  48. <div class="col-md-8">
  49. <div class="form-group">
  50. <h3>Current Photo</h3>
  51. <img src="<?php echo $fetch['photo']?>" height="120" width="150" />
  52. <input type="hidden" name="previous" value="<?php echo $fetch['photo']?>"/>
  53. <h3>New Photo</h3>
  54. <input type="file" class="form-control" name="photo" value="<?php echo $fetch['photo']?>" required="required"/>
  55. </div>
  56. <div class="form-group">
  57. <label>Firstname</label>
  58. <input type="hidden" value="<?php echo $fetch['user_id']?>" name="user_id"/>
  59. <input type="text" class="form-control" value="<?php echo $fetch['firstname']?>" name="firstname" required="required"/>
  60. </div>
  61. <div class="form-group">
  62. <label>Lastname</label>
  63. <input type="text" class="form-control" value="<?php echo $fetch['lastname']?>" name="lastname" required="required"/>
  64. </div>
  65. </div>
  66. </div>
  67. <br style="clear:both;"/>
  68. <div class="modal-footer">
  69. <button class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  70. <button class="btn btn-warning" name="edit"><span class="glyphicon glyphicon-save"></span> Update</button>
  71. </div>
  72. </form>
  73. </div>
  74. </div>
  75. </div>
  76. </tr>
  77. <?php
  78. }
  79. ?>
  80. </tbody>
  81. </table>
  82. </div>
  83.  
  84. <div class="modal fade" id="form_modal" aria-hidden="true">
  85. <div class="modal-dialog">
  86. <div class="modal-content">
  87. <form method="POST" action="save.php" enctype="multipart/form-data">
  88. <div class="modal-header">
  89. <h3 class="modal-title">Add User</h3>
  90. </div>
  91. <div class="modal-body">
  92. <div class="col-md-2"></div>
  93. <div class="col-md-8">
  94. <div class="form-group">
  95. <label>Photo</label>
  96. <input type="file" class="form-control" name="photo" required="required"/>
  97. </div>
  98. <div class="form-group">
  99. <label>Firstname</label>
  100. <input type="text" class="form-control" name="firstname" required="required"/>
  101. </div>
  102. <div class="form-group">
  103. <label>Lastname</label>
  104. <input type="text" class="form-control" name="lastname" required="required"/>
  105. </div>
  106. </div>
  107. </div>
  108. <br style="clear:both;"/>
  109. <div class="modal-footer">
  110. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  111. <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
  112. </div>
  113. </form>
  114. </div>
  115. </div>
  116. </div>
  117.  
  118. <script src="js/jquery-3.2.1.min.js"></script>
  119. <script src="js/bootstrap.js"></script>
  120. </body>
  121. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the user information to the MySQLi database server. To do that just copy and write this block of codes inside the text editor, then save it as save.php.

  1. <?php
  2. require_once 'conn.php';
  3. if(ISSET($_POST['save'])){
  4. $image_name = $_FILES['photo']['name'];
  5. $image_temp = $_FILES['photo']['tmp_name'];
  6. $firstname = $_POST['firstname'];
  7. $lastname = $_POST['lastname'];
  8. $exp = explode(".", $image_name);
  9. $end = end($exp);
  10. $name = time().".".$end;
  11. $path = "upload/".$name;
  12. $allowed_ext = array("gif", "jpg", "jpeg", "png");
  13. if(in_array($end, $allowed_ext)){
  14. if(move_uploaded_file($image_temp, $path)){
  15. mysqli_query($conn, "INSERT INTO `user` VALUES('', '$firstname', '$lastname', '$path')") or die(mysqli_error());
  16. echo "<script>alert('User account saved!')</script>";
  17. header("location: index.php");
  18. }
  19. }else{
  20. echo "<script>alert('Image only')</script>";
  21. }
  22. }
  23. ?>

Creating the Main Function

This code contains the main function of the application. This code will update the user image when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as edit.php

  1. <?php
  2. require_once 'conn.php';
  3. if(ISSET($_POST['edit'])){
  4. $user_id = $_POST['user_id'];
  5. $image_name = $_FILES['photo']['name'];
  6. $image_temp = $_FILES['photo']['tmp_name'];
  7. $firstname = $_POST['firstname'];
  8. $lastname = $_POST['lastname'];
  9. $previous = $_POST['previous'];
  10. $exp = explode(".", $image_name);
  11. $end = end($exp);
  12. $name = time().".".$end;
  13. $path = "upload/".$name;
  14. $allowed_ext = array("gif", "jpg", "jpeg", "png");
  15. if(in_array($end, $allowed_ext)){
  16. if(unlink($previous)){
  17. if(move_uploaded_file($image_temp, $path)){
  18. mysqli_query($conn, "UPDATE `user` set `firstname` = '$firstname', `lastname` = '$lastname', `photo` = '$path' WHERE `user_id` = '$user_id'") or die(mysqli_error());
  19. echo "<script>alert('User account updated!')</script>";
  20. header("location: index.php");
  21. }
  22. }
  23. }else{
  24. echo "<script>alert('Image only')</script>";
  25. }
  26. }
  27. ?>

There you have it we successfully created Simple Image Update using PHP. 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.