PHP - Simple Profile Update

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

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


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.