Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Benutzerkontenverwaltung, Rollen, Berechtigungen, Authentifizierung PHP und MySQL - Teil 6

Dies ist Teil 5 einer Reihe zum Erstellen eines Verwaltungssystems für Benutzerkonten in PHP. Die anderen Teile findest du hier:Teil 1, Teil 2, Teil 3, Teil 4 und Teil 5.

Erstellen Sie eine Datei namens editProfile.php in Ihrem Ordner admin/users.

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>

Jetzt präsentiert diese Seite ein Formular für den Benutzer, um seine Profilinformationen zu bearbeiten. Dies ist der Benutzeraktualisierungsfunktion sehr ähnlich, daher verwenden wir dieselbe Funktion, die Benutzer aktualisiert, um ein Benutzerprofil zu aktualisieren. Öffnen Sie also userLogic.php und fügen Sie diese if-Anweisung zu den if-Anweisungen im oberen Abschnitt der Datei hinzu.

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

Melden Sie sich jetzt mit Ihrem zuvor erstellten Administratorkonto an. Klicken Sie nach der Anmeldung auf Ihren Benutzernamen in der Navigationsleiste und wählen Sie „Profil“ aus der angezeigten Dropdown-Liste aus. Dadurch gelangen Sie zur Seite „Profil bearbeiten“. Ändern Sie die Informationen und klicken Sie auf die Schaltfläche „Aktualisieren“ und Ihr Benutzerkonto wird aktualisiert.

Wenn Sie Ihr Profil aktualisieren, wird eine Meldung angezeigt, die besagt, dass Sie das Konto erfolgreich aktualisiert haben, dieses Konto jedoch nicht in der Tabelle der Benutzerkonten aufgeführt wird. Das liegt daran, dass Sie der aktuell angemeldete Benutzer sind, sodass Ihr Konto nicht in der Kontentabelle angezeigt werden sollte, wenn Sie angemeldet sind. Es ist natürlich eine persönliche Präferenz, wenn Sie Ihr Konto zur Tabelle hinzufügen möchten, können Sie dies ändern entsprechenden Quellcode.

Schlussfolgerung

Vielen Dank, dass Sie diesem Tutorial gefolgt sind. Es war eine ziemlich lange Reise für mich, aber ich habe es genossen. Ich hoffe, Sie auch. Aufgrund der Länge dieses Tutorials habe ich möglicherweise vergessen, etwas hinzuzufügen oder zu entfernen. Vielleicht habe ich an der einen oder anderen Stelle einen Fehler gemacht. Wenn Sie solche Fehler entdeckt haben, hinterlassen Sie bitte einen Kommentar im Kommentarbereich, damit ich sie beheben kann.

Bitte unterstützen Sie durch Teilen.

Ich wünsche Ihnen einen wunderschönen Tag!