Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

Quản lý tài khoản người dùng, vai trò, quyền, xác thực PHP và MySQL - Phần 6

Đây là phần 5 của loạt bài về cách tạo hệ thống quản lý tài khoản người dùng bằng PHP. Bạn có thể tìm các phần khác tại đây:part1, part2, part 3, part 4 và part 5.

Tạo một tệp có tên editProfile.php trong thư mục quản trị / người dùng của bạn.

editProfile.php:

<?php include('../../config.php'); ?>
<?php include(INCLUDE_PATH . '/logic/common_functions.php'); ?>
<?php include(ROOT_PATH . '/admin/middleware.php'); ?>
<?php include(ROOT_PATH . '/admin/users/userLogic.php'); ?>
<?php
  $sql = "SELECT id, username, email, profile_picture FROM users WHERE id=?";
  $user = getSingleRecord($sql, 'i', [$_SESSION['user']['id']]);
  $roles = getMultipleRecords("SELECT * FROM roles");

  $user_id = $user['id'];
  $username = $user['username'];
  $email = $user['email'];
  $profile_picture = $user['profile_picture'];
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>UserAccounts - Edit Profile</title>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
    <!-- Custom styles -->
    <link rel="stylesheet" href="../../assets/css/style.css">
  </head>
  <body>
    <?php include(INCLUDE_PATH . "/layouts/admin_navbar.php") ?>

    <div class="container">
      <div class="row">

        <form action="editProfile.php" method="post" enctype="multipart/form-data">
          <input type="hidden" name="user_id" value="<?php echo $user_id ?>">
          <div class="col-md-8 col-md-offset-2">
            <h2 class="text-center">Edit Your Profile Info</h2>
            <hr>
              <div class="col-md-6" style="text-align: center;">
                  <?php if (isset($profile_picture)): ?>
                    <img src="<?php echo BASE_URL . '/assets/images/' . $profile_picture; ?>" id="profile_img" style="height: 150px; border-radius: 50%" alt="">
                  <?php else: ?>
                    <img src="http://via.placeholder.com/150x150" id="profile_img" style="height: 150px; border-radius: 50%" alt="">
                  <?php endif; ?>
                  <h3>Change Profile Picture</h3>
                  <!-- hidden file input to trigger with JQuery  -->
                  <input type="file" name="profile_picture" id="profile_input" value="" style="display: none;">
              </div>

              <div class="col-md-6">
                <div class="form-group <?php echo isset($errors['username']) ? 'has-error' : '' ?>">
                  <label class="control-label">Username</label>
                  <input type="text" name="username" value="<?php echo $username; ?>" class="form-control">
                  <?php if (isset($errors['username'])): ?>
                    <span class="help-block"><?php echo $errors['username'] ?></span>
                  <?php endif; ?>
                </div>
                <div class="form-group <?php echo isset($errors['email']) ? 'has-error' : '' ?>">
                  <label class="control-label">Email Address</label>
                  <input type="email" name="email" value="<?php echo $email; ?>" class="form-control">
                  <?php if (isset($errors['email'])): ?>
                    <span class="help-block"><?php echo $errors['email'] ?></span>
                  <?php endif; ?>
                </div>
                <div class="form-group <?php echo isset($errors['passwordOld']) ? 'has-error' : '' ?>">
                  <label class="control-label">Old Password</label>
                  <input type="password" name="passwordOld" class="form-control">
                  <?php if (isset($errors['passwordOld'])): ?>
                    <span class="help-block"><?php echo $errors['passwordOld'] ?></span>
                  <?php endif; ?>
                </div>
                <div class="form-group <?php echo isset($errors['password']) ? 'has-error' : '' ?>">
                  <label class="control-label">Password</label>
                  <input type="password" name="password" class="form-control">
                  <?php if (isset($errors['password'])): ?>
                    <span class="help-block"><?php echo $errors['password'] ?></span>
                  <?php endif; ?>
                </div>
                <div class="form-group <?php echo isset($errors['passwordConf']) ? 'has-error' : '' ?>">
                  <label class="control-label">Password confirmation</label>
                  <input type="password" name="passwordConf" class="form-control">
                  <?php if (isset($errors['passwordConf'])): ?>
                    <span class="help-block"><?php echo $errors['passwordConf'] ?></span>
                  <?php endif; ?>
                </div>
                <div class="form-group <?php echo isset($errors['role_id']) ? 'has-error' : '' ?>">
                  <label class="control-label">User Role</label>
                  <select class="form-control" name="role_id" >
                    <option value="" ></option>
                    <?php foreach ($roles as $role): ?>
                      <?php if ($role['name'] === $_SESSION['user']['role']): ?>
                        <option value="<?php echo $role['id'] ?>" selected><?php echo $role['name'] ?></option>
                      <?php else: ?>
                        <option value="<?php echo $role['id'] ?>"><?php echo $role['name'] ?></option>
                      <?php endif; ?>
                    <?php endforeach; ?>
                  </select>
                  <?php if (isset($errors['role_id'])): ?>
                    <span class="help-block"><?php echo $errors['role_id'] ?></span>
                  <?php endif; ?>
                </div>
                <div class="form-group">
                  <button type="submit" name="signup_btn" class="btn btn-danger pull-right">Delete Your Account</button>
                  <button type="submit" name="update_profile" class="btn btn-success">Update Profile</button>
                </div>
              </div>
          </div>
      </form>

      </div>
    </div>
  <?php include(INCLUDE_PATH . "/layouts/footer.php") ?>
  <script type="text/javascript" src="../../assets/js/display_profile_image.js"></script>

Bây giờ trang này trình bày một biểu mẫu để người dùng chỉnh sửa thông tin hồ sơ của họ. Điều này rất giống với tính năng người dùng cập nhật vì vậy chúng tôi sẽ sử dụng cùng một chức năng cập nhật người dùng để cập nhật hồ sơ người dùng. Vì vậy, hãy mở userLogic.php và thêm câu lệnh if này trong số các câu lệnh if xung quanh phần đầu của tệp.

userLogic.php:

// ... more code here

if (isset($_POST['update_profile'])) {
    $user_id = $_SESSION['user']['id'];
    if (!isset($user_id)) {
      $_SESSION['success_msg'] = "You have to be logged in to update your profile";
      header("location: " . BASE_URL . "login.php");
      exit(0);
    } else {
      updateUser($user_id); // Update logged in user profile
    }
}

// ... more code here ...

Bây giờ đăng nhập bằng tài khoản quản trị của bạn mà chúng tôi đã tạo trước đó. Sau khi đăng nhập, hãy nhấp vào tên người dùng của bạn trên thanh điều hướng và chọn "Hồ sơ" từ menu thả xuống xuất hiện. Thao tác này sẽ đưa bạn đến trang chỉnh sửa hồ sơ. Sửa đổi thông tin và nhấn nút cập nhật và tài khoản người dùng của bạn sẽ được cập nhật.

Khi bạn cập nhật hồ sơ của mình, một thông báo sẽ hiển thị cho biết bạn đã cập nhật tài khoản thành công nhưng bạn sẽ không thấy tài khoản đó được liệt kê trên bảng tài khoản người dùng. Đó là vì bạn là người dùng đang đăng nhập nên tài khoản của bạn sẽ không được hiển thị trên bảng tài khoản khi bạn đăng nhập. Tất nhiên đó là sở thích cá nhân, nếu bạn muốn thêm tài khoản của mình vào bảng, bạn có thể sửa đổi mã nguồn thích hợp.

Kết luận

Cảm ơn bạn rất nhiều vì đã theo dõi bài hướng dẫn này. Đó là một hành trình khá dài đối với tôi nhưng tôi rất thích nó. Tôi hi vọng bạn cũng làm được. Vì hướng dẫn này đã quá lâu, tôi có thể đã quên thêm hoặc bớt một cái gì đó. Tôi có thể đã mắc lỗi ở chỗ này hay chỗ khác. Nếu bạn phát hiện bất kỳ lỗi nào như vậy, vui lòng để lại bình luận trong phần nhận xét để tôi có thể sửa nó.

Hãy ủng hộ bằng cách chia sẻ.

Chúc một ngày tuyệt vời!


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySQL ROUND () Hàm - Làm tròn một số đến một số vị trí thập phân cho trước

  2. CHÈN ... TRÊN CẬP NHẬT KHÓA DUPLICATE với WHERE?

  3. Cách lấy ngày hiện tại trong MySQL

  4. Cấu hình mysql dừng ở máy chủ khởi động

  5. Khi nào sử dụng MyISAM và InnoDB?