Creating a Simple Image Update in PHP Tutorial

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 clicks the update button. The code use MySQLi UPDATE query to update the new user information base on the given user id. This is a user-friendly program feel free to modify and use it in your system.

We will be using PHP as a scripting language that interprets in the webserver such as XAMPP, wamp, etc. It is widely used by modern website applications 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

First, make sure that you have already started your XAMPP's Apache and MySQL. Then browse the PHPMyAdmin in a browser i.e. http://localhost/phpmyadmin. Then create a new database naming db_img_update and go to the SQL tab and copy/paste the script below and click "Go" button to execute the code.

  1. CREATE TABLE `user` (
  2. `firstname` varchar(50) NOT NULL,
  3. `lastname` varchar(50) NOT NULL,
  4. `photo` text NOT NULL

You can also import the provided SQL File provided along with the source code zip file insde the db folder. Click the Import tab at PHPMyadmin 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. if(!is_dir("./upload"))
  12. mkdir("upload");
  13. $path = "upload/".$name;
  14. $allowed_ext = array("gif", "jpg", "jpeg", "png");
  15. if(in_array($end, $allowed_ext)){
  16. if(move_uploaded_file($image_temp, $path)){
  17. mysqli_query($conn, "INSERT INTO `user` VALUES('', '$firstname', '$lastname', '$path')") or die(mysqli_error());
  18. echo "<script>alert('User account saved!')</script>";
  19. header("location: index.php");
  20. }
  21. }else{
  22. echo "<script>alert('Image only')</script>";
  23. }
  24. }
  25. ?>

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. if(!is_dir("./upload"))
  14. mkdir("upload");
  15. $path = "upload/".$name;
  16. $allowed_ext = array("gif", "jpg", "jpeg", "png");
  17. if(in_array($end, $allowed_ext)){
  18. if(unlink($previous)){
  19. if(move_uploaded_file($image_temp, $path)){
  20. mysqli_query($conn, "UPDATE `user` set `firstname` = '$firstname', `lastname` = '$lastname', `photo` = '$path' WHERE `user_id` = '$user_id'") or die(mysqli_error());
  21. echo "<script>alert('User account updated!')</script>";
  22. header("location: index.php");
  23. }
  24. }
  25. }else{
  26. echo "<script>alert('Image only')</script>";
  27. }
  28. }
  29. ?>

DEMO

There you have it we successfully created Simple Image Update using PHP. I hope that this simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!

Comments

Submitted byBj valenzuela (not verified)on Thu, 03/24/2022 - 23:47

Im getting this kind of error how to solver it sir? Warning: move_uploaded_file(upload/1648136579.jpg): Failed to open stream: Permission denied in C:\xampp\htdocs\demo\save.php on line 16 Warning: move_uploaded_file(): Unable to move "C:\xampp\tmp\phpF07B.tmp" to "upload/1648136579.jpg" in C:\xampp\htdocs\demo\save.php on line 16
Submitted byAnonymous (not verified)on Wed, 10/12/2022 - 15:42

good code
Submitted byAndrea75 (not verified)on Thu, 09/14/2023 - 02:08

Done! Ecxellentt

Add new comment