Add, Edit, Delete Record With User Profile in PHP/MySQL

In this tutorial we will create a simpleAdd, Edit, Delete, With User Profile In PHP/MySQL - PDO. The application works if the admin will create or add a new member in the list. Every member that the admin created is compose of their username, description and the profile image. And only the admin can access and delete some users in the system. The purpose of this system is to make the users work easily by identifying every persons profile. The application is programmed using Bootstrap Framework, PHP, and PDO.

Sample Code

Home.php - This script is for the list of members to be displayed and to be added.
  1. <div class="container">
  2. <h1 align="center">PHP/MySQL Add, Edit, Delete, With User Profile.</h1>
  3. <div class="page-header">
  4. <h1 class="h2">&nbsp; List of Members<a class="btn btn-success" href="addmember.php" style="margin-left: 770px;"><span class="glyphicon glyphicon-user"></span>&nbsp; Add Member</a></h1><hr>
  5. </div>
  6. <div class="row">
  7. <?php
  8. $stmt = $DB_con->prepare('SELECT userid, username, description, userprofile FROM users ORDER BY userid DESC');
  9. $stmt->execute();
  10. if($stmt->rowCount() > 0)
  11. {
  12. while($row=$stmt->fetch(PDO::FETCH_ASSOC))
  13. {
  14. extract($row);
  15. ?>
  16. <div class="col-xs-3">
  17. <h3 class="page-header" style="background-color:cadetblue" align="center"><?php echo $username."<br>".$description; ?></h3>
  18. <img src="uploads/<?php echo $row['userprofile']; ?>" class="img-rounded" width="250px" height="250px" /><hr>
  19. <p class="page-header" align="center">
  20. <span>
  21. <a class="btn btn-primary" href="editform.php?edit_id=<?php echo $row['userid']; ?>"><span class="glyphicon glyphicon-pencil"></span> Edit</a>
  22. <a class="btn btn-warning" href="?delete_id=<?php echo $row['userid']; ?>" title="click for delete" onclick="return confirm('Are You Sure You Want To Delete This User?')"><span class="glyphicon glyphicon-trash"></span> Delete</a>
  23. </span>
  24. </p>
  25. </div>
  26. <?php
  27. }
  28. }
  29. else
  30. {
  31. ?>
  32. <div class="col-xs-12">
  33. <div class="alert alert-warning">
  34. <span class="glyphicon glyphicon-info-sign"></span>&nbsp; No Data Found.
  35. </div>
  36. </div>
  37. <?php
  38. }
  39. ?>
  40. </div>
  41. </div>
And for the deleting of each user.
  1. <?php
  2. require_once 'dbcon.php';
  3.  
  4. if(isset($_GET['delete_id']))
  5. {
  6. $stmt_select = $DB_con->prepare('SELECT userprofile FROM users WHERE userid =:uid');
  7. $stmt_select->execute(array(':uid'=>$_GET['delete_id']));
  8. $imgRow=$stmt_select->fetch(PDO::FETCH_ASSOC);
  9. unlink("user_images/".$imgRow['userprofile']);
  10. $stmt_delete = $DB_con->prepare('DELETE FROM users WHERE userid =:uid');
  11. $stmt_delete->bindParam(':uid',$_GET['delete_id']);
  12. $stmt_delete->execute();
  13. header("Location: index.php");
  14. }
  15. ?>
Editform.php - And this script is for updating an existing member.
  1. <?php
  2. error_reporting( ~E_NOTICE );
  3. require_once 'dbcon.php';
  4.  
  5. if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
  6. {
  7. $id = $_GET['edit_id'];
  8. $stmt_edit = $DB_con->prepare('SELECT username, description, userprofile FROM users WHERE userid =:uid');
  9. $stmt_edit->execute(array(':uid'=>$id));
  10. $edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
  11. extract($edit_row);
  12. }
  13. else
  14. {
  15. header("Location: index.php");
  16. }
  17. if(isset($_POST['btn_save_updates']))
  18. {
  19. $username = $_POST['user_name'];
  20. $description = $_POST['description'];
  21. $imgFile = $_FILES['user_image']['name'];
  22. $tmp_dir = $_FILES['user_image']['tmp_name'];
  23. $imgSize = $_FILES['user_image']['size'];
  24. if($imgFile)
  25. {
  26. $upload_dir = 'uploads/';
  27. $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
  28. $valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
  29. $userprofile = rand(1000,1000000).".".$imgExt;
  30. if(in_array($imgExt, $valid_extensions))
  31. {
  32. if($imgSize < 5000000)
  33. {
  34. unlink($upload_dir.$edit_row['userprofile']);
  35. move_uploaded_file($tmp_dir,$upload_dir.$userprofile);
  36. }
  37. else
  38. {
  39. $errMSG = "Sorry, Your File Is Too Large To Upload. It Should Be Less Than 5MB.";
  40. }
  41. }
  42. else
  43. {
  44. $errMSG = "Sorry, only JPG, JPEG, PNG & GIF Extension Files Are Allowed.";
  45. }
  46. }
  47. else
  48. {
  49. $userprofile = $edit_row['userprofile'];
  50. }
  51. if(!isset($errMSG))
  52. {
  53. $stmt = $DB_con->prepare('UPDATE users SET username=:uname, description=:udes, userprofile=:upic WHERE userid=:uid');
  54. $stmt->bindParam(':uname',$username);
  55. $stmt->bindParam(':udes',$description);
  56. $stmt->bindParam(':upic',$userprofile);
  57. $stmt->bindParam(':uid',$id);
  58.  
  59. if($stmt->execute()){
  60. ?>
  61. <script>
  62. alert('Successfully Updated...');
  63. window.location.href='home.php';
  64. </script>
  65. <?php
  66. }
  67. else{
  68. $errMSG = "Sorry User Could Not Be Updated!";
  69. }
  70. }
  71. }
  72. ?>
Addmember.php - This is for creating a new member.
  1. <?php
  2. error_reporting( ~E_NOTICE );
  3. require_once 'dbcon.php';
  4.  
  5. if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
  6. {
  7. $id = $_GET['edit_id'];
  8. $stmt_edit = $DB_con->prepare('SELECT username, description, userprofile FROM users WHERE userid =:uid');
  9. $stmt_edit->execute(array(':uid'=>$id));
  10. $edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
  11. extract($edit_row);
  12. }
  13. else
  14. {
  15. header("Location: index.php");
  16. }
  17. if(isset($_POST['btn_save_updates']))
  18. {
  19. $username = $_POST['user_name'];
  20. $description = $_POST['description'];
  21. $imgFile = $_FILES['user_image']['name'];
  22. $tmp_dir = $_FILES['user_image']['tmp_name'];
  23. $imgSize = $_FILES['user_image']['size'];
  24. if($imgFile)
  25. {
  26. $upload_dir = 'uploads/';
  27. $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
  28. $valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
  29. $userprofile = rand(1000,1000000).".".$imgExt;
  30. if(in_array($imgExt, $valid_extensions))
  31. {
  32. if($imgSize < 5000000)
  33. {
  34. unlink($upload_dir.$edit_row['userprofile']);
  35. move_uploaded_file($tmp_dir,$upload_dir.$userprofile);
  36. }
  37. else
  38. {
  39. $errMSG = "Sorry, Your File Is Too Large To Upload. It Should Be Less Than 5MB.";
  40. }
  41. }
  42. else
  43. {
  44. $errMSG = "Sorry, only JPG, JPEG, PNG & GIF Extension Files Are Allowed.";
  45. }
  46. }
  47. else
  48. {
  49. $userprofile = $edit_row['userprofile'];
  50. }
  51. if(!isset($errMSG))
  52. {
  53. $stmt = $DB_con->prepare('UPDATE users SET username=:uname, description=:udes, userprofile=:upic WHERE userid=:uid');
  54. $stmt->bindParam(':uname',$username);
  55. $stmt->bindParam(':udes',$description);
  56. $stmt->bindParam(':upic',$userprofile);
  57. $stmt->bindParam(':uid',$id);
  58.  
  59. if($stmt->execute()){
  60. ?>
  61. <script>
  62. alert('Successfully Updated...');
  63. window.location.href='home.php';
  64. </script>
  65. <?php
  66. }
  67. else{
  68. $errMSG = "Sorry User Could Not Be Updated!";
  69. }
  70. }
  71. }
  72. ?>
resultHope that you learn in this tutorial. And for more updates and programming tutorials don't hesitate to ask and we will answer your questions and suggestions. Don't forget to LIKE & SHARE this website.

Comments

This is a very useful project, you can put search box above and pagination

Add new comment