Skip to main content

PHP - Simple Profile Update


In this tutorial we will create a Simple Profile Update using PHP. This code will launch a modal to update the user profile when the user click the update button. The code use MySQLi UPDATE query to update the user current profile into a brand new one 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_profile, 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_profile");
  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 Profile Update</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-2"></div>
  18. <div class="col-md-8">
  19. <nav class="navbar navbar-default">
  20. <div class="container-fluid alert-info">
  21. <a class="navbar-brand" href="https://sourcecodester.com">FaceProfile</a>
  22. </div>
  23. </nav>
  24. <button class="btn btn-warning" data-toggle="modal" data-target="#form_modal">Edit Profile</button>
  25. <?php
  26. require'conn.php';
  27. $query=mysqli_query($conn, "SELECT * FROM `user` WHERE `user_id` = '1'") or die(mysqli_error());
  28. $fetch=mysqli_fetch_array($query);
  29. ?>
  30. <br style="clear:both;"/><br />
  31. <div class="col-md-2"></div>
  32. <div class="col-md-8">
  33. <img src="profile/<?php echo $fetch['profile']?>" width="250px"/>
  34. <h4><?php echo $fetch['firstname']." ".$fetch['lastname']?></h4>
  35. </div>
  36. </div>
  37. </div>
  38. <div class="modal fade" id="form_modal" aria-hidden="true">
  39. <div class="modal-dialog">
  40. <div class="modal-content">
  41. <form method="POST" enctype="multipart/form-data" action="update_profile.php">
  42. <div class="modal-header">
  43. <h3 class="modal-title">Update Profile</h3>
  44. </div>
  45. <div class="modal-body">
  46. <div class="col-md-2"></div>
  47. <div class="col-md-8">
  48. <label>Current Profile</label>
  49. <img src="profile/<?php echo $fetch['profile']?>" width="250px"/>
  50. <hr style="border-top:1px solid #000;"/>
  51. <div class="form-group">
  52. <label>New Profile</label>
  53. <input type="file" class="form-control" name="profile" required="required"/>
  54. <input type="hidden" class="form-control" name="current" value="<?php echo $fetch['profile']?>" required="required"/>
  55. </div>
  56. <div class="form-group">
  57. <label>Firstname</label>
  58. <input type="text" class="form-control" name="firstname" required="required"/>
  59. </div>
  60. <div class="form-group">
  61. <label>Lastname</label>
  62. <input type="text" class="form-control" name="lastname" required="required"/>
  63. </div>
  64. </div>
  65. </div>
  66. <br style="clear:both;"/>
  67. <div class="modal-footer">
  68. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  69. <button name="update" class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span> Update</button>
  70. </div>
  71. </form>
  72. </div>
  73. </div>
  74. </div>
  75. <script src="js/jquery-3.2.1.min.js"></script>
  76. <script src="js/bootstrap.js"></script>
  77. </body>
  78. </html>

Creating the Main Function

This code contains the main function of the application. This code will update the user profile 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 update_profile.php

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

There you have it we successfully created Simple Profile 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.

Submitted byvytas (not verified)on Fri, 12/06/2019 - 14:13

graet source code thx

Add new comment

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