PHP - Simple Image Update

Submitted by: 
Language: 
Visitors have accessed this post 599 times.

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


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.