Advertisement
tanmyid

ubah.php

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