Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $id_user = $_GET['id'];
- $tampilUser = $conn->query("SELECT * FROM tb_user WHERE id_user = $id_user") or die(mysqli_error($conn));
- $pecahUser = $tampilUser->fetch_assoc();
- if (isset($_POST['ubah'])) {
- $nama = htmlspecialchars($_POST['nama']);
- $username = htmlspecialchars($_POST['username']);
- // $password = htmlspecialchars($_POST['password']);
- if ($_POST['password'] === $pecahUser['password']) {
- $pass = $_POST['password'];
- } else {
- $pass = password_hash( $_POST['password'], PASSWORD_DEFAULT);
- }
- $role = htmlspecialchars($_POST['role']);
- // Upload Foto
- if (!empty($_FILES['foto']['name'])) {
- $foto = $_FILES['foto']['name'];
- $namaSementara = $_FILES['foto']['tmp_name'];
- $targetDir = "./img/user/";
- move_uploaded_file($namaSementara, $targetDir . $foto);
- // $targetFile = $targetDir . basename($foto);
- // $uploadOk = 1;
- // $imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
- // cek file dalam bentuk foto
- } else {
- $foto = $pecahUser['foto'];
- }
- // Query
- $sql = $conn->prepare("UPDATE tb_user SET
- nama = ?,
- username = ?,
- password = ?,
- role = ?,
- foto = ?
- WHERE id_user = ?");
- $sql->bind_param("sssssi", $nama, $username, $pass, $role, $foto, $id_user);
- if ($sql->execute()) {
- echo "<script>alert('Data Berhasil Diubah.');window.location='?p=Data User';</script>";
- } else {
- // Delete the uploaded file if the update fails (optional)
- if (!empty($_FILES['foto']['name']) && file_exists($targetFile)) {
- unlink($targetFile);
- }
- echo "<script>alert('Data Gagal Diubah. Error: " . $sql->error . "');window.location='?p=Data User';</script>";
- }
- // Close the statement
- $sql->close();
- }
- ?>
- <center>
- <div class="rangkasurat">
- <table width="70%">
- <tr class="noborder">
- <td class="noborder"><img src="./img/Logo SMKN 3 Kudus.png" width="110px"></td>
- <td class="tengah noborder">
- <h2>PERPUSTAKAAN</h2>
- <h1>SMK NEGERI 3 KUDUS</h1>
- </td>
- </tr>
- </table>
- </center>
- <h1 class="mt-4">Ubah Data User</h1>
- <ol class="breadcrumb mb-4">
- <li class="breadcrumb-item"><a href="index_petugas.php">Dashboad</a></li>
- <li class="breadcrumb-item"><a href="index_petugas.php?p=Data User">Data User</a></li>
- <li class="breadcrumb-item active">Ubah Data Anggota</li>
- </ol>
- <div class="card-header mb-5">
- <form action="" method="post" enctype="multipart/form-data">
- <div class="form-group">
- <label class="small mb-1" for="nama">Nama</label>
- <input class="form-control" id="nama" value="<?= $pecahUser['nama']; ?>" name="nama" type="text" placeholder="Masukan Nama User" />
- </div>
- <div class="form-group">
- <label class="small mb-1" for="username">Username</label>
- <input class="form-control" id="username" value="<?= $pecahUser['username']; ?>" name="username" type="text" placeholder="Masukan Username" />
- </div>
- <div class="form-group">
- <label class="small mb-1" for="password">Password</label>
- <input class="form-control" id="password" value="<?php echo isset($pecahUser['password']) ? $pecahUser['password'] : ''; ?>" name="password" type="password" placeholder="Masukan Password" />
- <div class="input-group-append">
- <span class="input-group-text">
- <input type="checkbox" onclick="togglePassword()"> Show
- </span>
- </div>
- </div>
- <div class="form-group">
- <label class="small mb-1" for="role">Role</label>
- <select name="role" id="role" class="form-control">
- <option value="">-- Pilih Role --</option>
- <option value="petugas" <?php echo ($pecahUser['role'] == 'petugas') ? 'selected' : ''; ?>>Petugas</option>
- <option value="kepala" <?php echo ($pecahUser['role'] == 'kepala') ? 'selected' : ''; ?>>Kepala Perpus</option>
- <option value="anggota" <?php echo ($pecahUser['role'] == 'anggota') ? 'selected' : ''; ?>>Anggota</option>
- </select>
- </div>
- <div class="form-group">
- <label class="small mb-1" for="currentFoto">Current Foto</label>
- <?php if (isset($pecahUser['foto']) && !empty($pecahUser['foto'])) : ?>
- <img src="./img/user/<?php echo $pecahUser['foto']; ?>" alt="Current Foto" width="100">
- <?php else : ?>
- <p>No current photo available</p>
- <?php endif; ?>
- </div>
- <div class="form-group">
- <label class="small mb-1" for="foto">Foto</label>
- <input type="file" class="form-control-file" id="foto" name="foto">
- </div>
- <div class="form-group">
- <button type="submit" class="btn btn-primary" name="ubah">Ubah Data</button>
- </div>
- </form>
- </div>
- <script>
- function togglePassword() {
- // Get the password input field
- var passwordField = document.getElementById('password');
- // Check if the current type is 'password'
- if (passwordField.type === 'password') {
- // Change it to 'text' to make the password visible
- passwordField.type = 'text';
- } else {
- // Change it back to 'password' to hide the password
- passwordField.type = 'password';
- }
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement