Advertisement
tanmyid

Ubah New

Jan 1st, 2024 (edited)
1,154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.75 KB | None | 0 0
  1. <?php
  2.  
  3. $id_user = $_GET['id'];
  4. $tampilUser = $conn->query("SELECT * FROM tb_user WHERE id_user = $id_user") or die(mysqli_error($conn));
  5. $pecahUser = $tampilUser->fetch_assoc();
  6.  
  7. if (isset($_POST['ubah'])) {
  8.     $nama = htmlspecialchars($_POST['nama']);
  9.     $username = htmlspecialchars($_POST['username']);
  10.     // $password = htmlspecialchars($_POST['password']);
  11.     if ($_POST['password'] === $pecahUser['password']) {
  12.         $pass = $_POST['password'];
  13.     } else {
  14.         $pass = password_hash( $_POST['password'], PASSWORD_DEFAULT);
  15.     }
  16.     $role = htmlspecialchars($_POST['role']);
  17.  
  18.  
  19.     // Upload Foto
  20.     if (!empty($_FILES['foto']['name'])) {
  21.         $foto = $_FILES['foto']['name'];
  22.         $namaSementara = $_FILES['foto']['tmp_name'];
  23.         $targetDir = "./img/user/";
  24.         move_uploaded_file($namaSementara, $targetDir . $foto);
  25.         // $targetFile = $targetDir . basename($foto);
  26.         // $uploadOk = 1;
  27.         // $imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
  28.  
  29.         // cek file dalam bentuk foto
  30.     } else {
  31.         $foto = $pecahUser['foto'];
  32.     }
  33.  
  34.     // Query
  35.  
  36.  
  37.     $sql = $conn->prepare("UPDATE tb_user SET
  38.                        nama = ?,
  39.                        username = ?,
  40.                        password = ?,
  41.                        role = ?,
  42.                        foto = ?
  43.                        WHERE id_user = ?");
  44.     $sql->bind_param("sssssi", $nama, $username, $pass, $role, $foto, $id_user);
  45.  
  46.  
  47.     if ($sql->execute()) {
  48.         echo "<script>alert('Data Berhasil Diubah.');window.location='?p=Data User';</script>";
  49.     } else {
  50.         // Delete the uploaded file if the update fails (optional)
  51.         if (!empty($_FILES['foto']['name']) && file_exists($targetFile)) {
  52.             unlink($targetFile);
  53.         }
  54.  
  55.         echo "<script>alert('Data Gagal Diubah. Error: " . $sql->error . "');window.location='?p=Data User';</script>";
  56.     }
  57.  
  58.     // Close the statement
  59.     $sql->close();
  60. }
  61. ?>
  62. <center>
  63.     <div class="rangkasurat">
  64.         <table width="70%">
  65.             <tr class="noborder">
  66.                 <td class="noborder"><img src="./img/Logo SMKN 3 Kudus.png" width="110px"></td>
  67.                 <td class="tengah noborder">
  68.                     <h2>PERPUSTAKAAN</h2>
  69.                     <h1>SMK NEGERI 3 KUDUS</h1>
  70.                 </td>
  71.             </tr>
  72.         </table>
  73. </center>
  74.  
  75. <h1 class="mt-4">Ubah Data User</h1>
  76. <ol class="breadcrumb mb-4">
  77.     <li class="breadcrumb-item"><a href="index_petugas.php">Dashboad</a></li>
  78.     <li class="breadcrumb-item"><a href="index_petugas.php?p=Data User">Data User</a></li>
  79.     <li class="breadcrumb-item active">Ubah Data Anggota</li>
  80. </ol>
  81. <div class="card-header mb-5">
  82.  
  83.     <form action="" method="post" enctype="multipart/form-data">
  84.         <div class="form-group">
  85.             <label class="small mb-1" for="nama">Nama</label>
  86.             <input class="form-control" id="nama" value="<?= $pecahUser['nama']; ?>" name="nama" type="text" placeholder="Masukan Nama User" />
  87.         </div>
  88.         <div class="form-group">
  89.             <label class="small mb-1" for="username">Username</label>
  90.             <input class="form-control" id="username" value="<?= $pecahUser['username']; ?>" name="username" type="text" placeholder="Masukan Username" />
  91.         </div>
  92.         <div class="form-group">
  93.             <label class="small mb-1" for="password">Password</label>
  94.             <input class="form-control" id="password" value="<?php echo isset($pecahUser['password']) ? $pecahUser['password'] : ''; ?>" name="password" type="password" placeholder="Masukan Password" />
  95.             <div class="input-group-append">
  96.                 <span class="input-group-text">
  97.                     <input type="checkbox" onclick="togglePassword()"> Show
  98.                 </span>
  99.             </div>
  100.         </div>
  101.         <div class="form-group">
  102.             <label class="small mb-1" for="role">Role</label>
  103.             <select name="role" id="role" class="form-control">
  104.                 <option value="">-- Pilih Role --</option>
  105.                 <option value="petugas" <?php echo ($pecahUser['role'] == 'petugas') ? 'selected' : ''; ?>>Petugas</option>
  106.                 <option value="kepala" <?php echo ($pecahUser['role'] == 'kepala') ? 'selected' : ''; ?>>Kepala Perpus</option>
  107.                 <option value="anggota" <?php echo ($pecahUser['role'] == 'anggota') ? 'selected' : ''; ?>>Anggota</option>
  108.             </select>
  109.         </div>
  110.         <div class="form-group">
  111.             <label class="small mb-1" for="currentFoto">Current Foto</label>
  112.             <?php if (isset($pecahUser['foto']) && !empty($pecahUser['foto'])) : ?>
  113.                 <img src="./img/user/<?php echo $pecahUser['foto']; ?>" alt="Current Foto" width="100">
  114.             <?php else : ?>
  115.                 <p>No current photo available</p>
  116.             <?php endif; ?>
  117.         </div>
  118.         <div class="form-group">
  119.             <label class="small mb-1" for="foto">Foto</label>
  120.             <input type="file" class="form-control-file" id="foto" name="foto">
  121.         </div>
  122.         <div class="form-group">
  123.             <button type="submit" class="btn btn-primary" name="ubah">Ubah Data</button>
  124.         </div>
  125.     </form>
  126. </div>
  127. <script>
  128.     function togglePassword() {
  129.         // Get the password input field
  130.         var passwordField = document.getElementById('password');
  131.  
  132.         // Check if the current type is 'password'
  133.         if (passwordField.type === 'password') {
  134.             // Change it to 'text' to make the password visible
  135.             passwordField.type = 'text';
  136.         } else {
  137.             // Change it back to 'password' to hide the password
  138.             passwordField.type = 'password';
  139.         }
  140.     }
  141. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement