• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

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

Pruned3250

Programming Paradigm Specialist
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
45
Likes
114
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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. if

    (

    isset

    (

    $_GET

    [

    'delete_id'

    ]

    )

    )
  4. {
  5. $stmt_select

    =

    $DB_con

    ->

    prepare

    (

    'SELECT userprofile FROM users WHERE userid =:uid'

    )

    ;
  6. $stmt_select

    ->

    execute

    (

    array

    (

    ':uid'

    =>

    $_GET

    [

    'delete_id'

    ]

    )

    )

    ;
  7. $imgRow

    =

    $stmt_select

    ->

    fetch

    (

    PDO::

    FETCH_ASSOC

    )

    ;
  8. unlink

    (

    "user_images/"

    .

    $imgRow

    [

    'userprofile'

    ]

    )

    ;
  9. $stmt_delete

    =

    $DB_con

    ->

    prepare

    (

    'DELETE FROM users WHERE userid =:uid'

    )

    ;
  10. $stmt_delete

    ->

    bindParam

    (

    ':uid'

    ,

    $_GET

    [

    'delete_id'

    ]

    )

    ;
  11. $stmt_delete

    ->

    execute

    (

    )

    ;
  12. header

    (

    "Location: index.php"

    )

    ;
  13. }
  14. ?>

Editform.php - And this script is for updating an existing member.

  1. <?php
  2. error_reporting

    (

    ~E_NOTICE

    )

    ;
  3. require_once

    'dbcon.php'

    ;

  4. if

    (

    isset

    (

    $_GET

    [

    'edit_id'

    ]

    )

    &&

    !

    empty

    (

    $_GET

    [

    'edit_id'

    ]

    )

    )
  5. {
  6. $id

    =

    $_GET

    [

    'edit_id'

    ]

    ;
  7. $stmt_edit

    =

    $DB_con

    ->

    prepare

    (

    'SELECT username, description, userprofile FROM users WHERE userid =:uid'

    )

    ;
  8. $stmt_edit

    ->

    execute

    (

    array

    (

    ':uid'

    =>

    $id

    )

    )

    ;
  9. $edit_row

    =

    $stmt_edit

    ->

    fetch

    (

    PDO::

    FETCH_ASSOC

    )

    ;
  10. extract

    (

    $edit_row

    )

    ;
  11. }
  12. else
  13. {
  14. header

    (

    "Location: index.php"

    )

    ;
  15. }
  16. if

    (

    isset

    (

    $_POST

    [

    'btn_save_updates'

    ]

    )

    )
  17. {
  18. $username

    =

    $_POST

    [

    'user_name'

    ]

    ;
  19. $description

    =

    $_POST

    [

    'description'

    ]

    ;
  20. $imgFile

    =

    $_FILES

    [

    'user_image'

    ]

    [

    'name'

    ]

    ;
  21. $tmp_dir

    =

    $_FILES

    [

    'user_image'

    ]

    [

    'tmp_name'

    ]

    ;
  22. $imgSize

    =

    $_FILES

    [

    'user_image'

    ]

    [

    'size'

    ]

    ;
  23. if

    (

    $imgFile

    )
  24. {
  25. $upload_dir

    =

    'uploads/'

    ;
  26. $imgExt

    =

    strtolower

    (

    pathinfo

    (

    $imgFile

    ,

    PATHINFO_EXTENSION)

    )

    ;
  27. $valid_extensions

    =

    array

    (

    'jpeg'

    ,

    'jpg'

    ,

    'png'

    ,

    'gif'

    )

    ;
  28. $userprofile

    =

    rand

    (

    1000

    ,

    1000000

    )

    .

    "."

    .

    $imgExt

    ;
  29. if

    (

    in_array

    (

    $imgExt

    ,

    $valid_extensions

    )

    )
  30. {
  31. if

    (

    $imgSize

    <

    5000000

    )
  32. {
  33. unlink

    (

    $upload_dir

    .

    $edit_row

    [

    'userprofile'

    ]

    )

    ;
  34. move_uploaded_file

    (

    $tmp_dir

    ,

    $upload_dir

    .

    $userprofile

    )

    ;
  35. }
  36. else
  37. {
  38. $errMSG

    =

    "Sorry, Your File Is Too Large To Upload. It Should Be Less Than 5MB."

    ;
  39. }
  40. }
  41. else
  42. {
  43. $errMSG

    =

    "Sorry, only JPG, JPEG, PNG & GIF Extension Files Are Allowed."

    ;
  44. }
  45. }
  46. else
  47. {
  48. $userprofile

    =

    $edit_row

    [

    'userprofile'

    ]

    ;
  49. }
  50. if

    (

    !

    isset

    (

    $errMSG

    )

    )
  51. {
  52. $stmt

    =

    $DB_con

    ->

    prepare

    (

    'UPDATE users SET username=:uname, description=:udes, userprofile=:upic WHERE userid=:uid'

    )

    ;
  53. $stmt

    ->

    bindParam

    (

    ':uname'

    ,

    $username

    )

    ;
  54. $stmt

    ->

    bindParam

    (

    ':udes'

    ,

    $description

    )

    ;
  55. $stmt

    ->

    bindParam

    (

    ':upic'

    ,

    $userprofile

    )

    ;
  56. $stmt

    ->

    bindParam

    (

    ':uid'

    ,

    $id

    )

    ;

  57. if

    (

    $stmt

    ->

    execute

    (

    )

    )

    {
  58. ?>
  59. <script>
  60. alert('Successfully Updated...');
  61. window.location.href='home.php';
  62. </script>
  63. <?php
  64. }
  65. else

    {
  66. $errMSG

    =

    "Sorry User Could Not Be Updated!"

    ;
  67. }
  68. }
  69. }
  70. ?>

Addmember.php - This is for creating a new member.

  1. <?php
  2. error_reporting

    (

    ~E_NOTICE

    )

    ;
  3. require_once

    'dbcon.php'

    ;

  4. if

    (

    isset

    (

    $_GET

    [

    'edit_id'

    ]

    )

    &&

    !

    empty

    (

    $_GET

    [

    'edit_id'

    ]

    )

    )
  5. {
  6. $id

    =

    $_GET

    [

    'edit_id'

    ]

    ;
  7. $stmt_edit

    =

    $DB_con

    ->

    prepare

    (

    'SELECT username, description, userprofile FROM users WHERE userid =:uid'

    )

    ;
  8. $stmt_edit

    ->

    execute

    (

    array

    (

    ':uid'

    =>

    $id

    )

    )

    ;
  9. $edit_row

    =

    $stmt_edit

    ->

    fetch

    (

    PDO::

    FETCH_ASSOC

    )

    ;
  10. extract

    (

    $edit_row

    )

    ;
  11. }
  12. else
  13. {
  14. header

    (

    "Location: index.php"

    )

    ;
  15. }
  16. if

    (

    isset

    (

    $_POST

    [

    'btn_save_updates'

    ]

    )

    )
  17. {
  18. $username

    =

    $_POST

    [

    'user_name'

    ]

    ;
  19. $description

    =

    $_POST

    [

    'description'

    ]

    ;
  20. $imgFile

    =

    $_FILES

    [

    'user_image'

    ]

    [

    'name'

    ]

    ;
  21. $tmp_dir

    =

    $_FILES

    [

    'user_image'

    ]

    [

    'tmp_name'

    ]

    ;
  22. $imgSize

    =

    $_FILES

    [

    'user_image'

    ]

    [

    'size'

    ]

    ;
  23. if

    (

    $imgFile

    )
  24. {
  25. $upload_dir

    =

    'uploads/'

    ;
  26. $imgExt

    =

    strtolower

    (

    pathinfo

    (

    $imgFile

    ,

    PATHINFO_EXTENSION)

    )

    ;
  27. $valid_extensions

    =

    array

    (

    'jpeg'

    ,

    'jpg'

    ,

    'png'

    ,

    'gif'

    )

    ;
  28. $userprofile

    =

    rand

    (

    1000

    ,

    1000000

    )

    .

    "."

    .

    $imgExt

    ;
  29. if

    (

    in_array

    (

    $imgExt

    ,

    $valid_extensions

    )

    )
  30. {
  31. if

    (

    $imgSize

    <

    5000000

    )
  32. {
  33. unlink

    (

    $upload_dir

    .

    $edit_row

    [

    'userprofile'

    ]

    )

    ;
  34. move_uploaded_file

    (

    $tmp_dir

    ,

    $upload_dir

    .

    $userprofile

    )

    ;
  35. }
  36. else
  37. {
  38. $errMSG

    =

    "Sorry, Your File Is Too Large To Upload. It Should Be Less Than 5MB."

    ;
  39. }
  40. }
  41. else
  42. {
  43. $errMSG

    =

    "Sorry, only JPG, JPEG, PNG & GIF Extension Files Are Allowed."

    ;
  44. }
  45. }
  46. else
  47. {
  48. $userprofile

    =

    $edit_row

    [

    'userprofile'

    ]

    ;
  49. }
  50. if

    (

    !

    isset

    (

    $errMSG

    )

    )
  51. {
  52. $stmt

    =

    $DB_con

    ->

    prepare

    (

    'UPDATE users SET username=:uname, description=:udes, userprofile=:upic WHERE userid=:uid'

    )

    ;
  53. $stmt

    ->

    bindParam

    (

    ':uname'

    ,

    $username

    )

    ;
  54. $stmt

    ->

    bindParam

    (

    ':udes'

    ,

    $description

    )

    ;
  55. $stmt

    ->

    bindParam

    (

    ':upic'

    ,

    $userprofile

    )

    ;
  56. $stmt

    ->

    bindParam

    (

    ':uid'

    ,

    $id

    )

    ;

  57. if

    (

    $stmt

    ->

    execute

    (

    )

    )

    {
  58. ?>
  59. <script>
  60. alert('Successfully Updated...');
  61. window.location.href='home.php';
  62. </script>
  63. <?php
  64. }
  65. else

    {
  66. $errMSG

    =

    "Sorry User Could Not Be Updated!"

    ;
  67. }
  68. }
  69. }
  70. ?>

2016-12-08_15_04_35-.png

Hope 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.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,513

356,535

356,560

Top
Raidforums