Advertisement
0xCor3

Untitled

Jul 27th, 2021
1,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.71 KB | None | 0 0
  1. <?php
  2. class CRUD {
  3.     protected $dbhost = 'localhost'; // database host
  4.     protected $dbuser = 'root'; // database username
  5.     protected $dbpass = ''; // database password
  6.     protected $dbname = 'crud_irul'; // database name
  7.     protected $mysqli;
  8.     public $message;
  9.  
  10.     public function __construct () // koneksi ke database
  11.     {
  12.         $mysqli = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
  13.         if($mysqli->connect_errno) die("Failed to connect DB: ".$mysqli->connect_error);
  14.         $this->mysqli = $mysqli;
  15.     }
  16.     public function create_data ($nim, $nama, $alamat, $jurusan) // untuk menambah data
  17.     {
  18.         $add = $this->mysqli->prepare("INSERT INTO `mahasiswa` (`nim`, `nama_lengkap`, `alamat`, `jurusan`) VALUES (?, ?, ?, ?)");
  19.         $add->bind_param("isss", $nim, $nama, $alamat, $jurusan);
  20.         $this->message = $add->execute();
  21.         $add->close();
  22.         return $this->message;
  23.     }
  24.     public function read_data ($nim = '') // untuk meload data yang berada di database
  25.     {
  26.         if (!empty($nim)){
  27.             $read = $this->mysqli->prepare("SELECT * FROM `mahasiswa` WHERE `nim` = ?");
  28.             $read->bind_param("i", $nim);
  29.             $this->message = $read->execute();
  30.             $result = $read->get_result();
  31.             return $result->fetch_assoc();
  32.         } else {
  33.             return $this->mysqli->query("SELECT * FROM `mahasiswa`")->fetch_all(MYSQLI_ASSOC);
  34.         }
  35.     }
  36.     public function update_data ($nim, $nama, $alamat, $jurusan) // untuk mengupdate data yang ada
  37.     {
  38.         $add = $this->mysqli->prepare("UPDATE `mahasiswa` SET `nama_lengkap` = ?, `alamat` = ?, `jurusan` = ? WHERE `mahasiswa`.`nim` = ?");
  39.         $add->bind_param("sssi", $nama, $alamat, $jurusan, $nim);
  40.         $this->message = $add->execute();
  41.         $add->close();
  42.         return $this->message;
  43.     }
  44.     public function delete_data ($nim) // untuk menghapus data yang ada
  45.     {
  46.         $del = $this->mysqli->prepare("DELETE FROM `mahasiswa` WHERE `mahasiswa`.`nim` = ?");
  47.         $del->bind_param("i", $nim);
  48.         $this->message = $del->execute();
  49.         $del->close();
  50.         return $this->message;
  51.     }
  52. }
  53. $crud = new CRUD; // inisialisasi kelas CRUD
  54. $i = 1;
  55. ?>
  56. <html>
  57.     <head>
  58.         <title>Simple CRUD</title>
  59.         <style>
  60.             th {
  61.                 width: 200px;
  62.                 text-align: left;
  63.             }
  64.         </style>
  65.     </head>
  66.     <body>
  67.         <?php
  68.             if(isset($_GET['mode']) && !empty($_GET['mode'])){
  69.                 if($_GET['mode'] == 'tambah'){
  70.                     if(isset($_POST['submit'])){
  71.                         $crud->create_data(trim($_POST['nim']), trim($_POST['nama']), trim($_POST['alamat']), trim($_POST['jurusan']));
  72.                         echo $crud->message == 1 ? "<script>alert('sukses tambah data')</script>" : "<script>alert('gagal tambah data')</script>";
  73.                     }
  74.         ?>
  75.                     <h3>Tambah Data | <a href="./">Home</a></h3>
  76.                     <form method="POST" action="./?mode=tambah">
  77.                         <table>
  78.                             <tr>
  79.                                 <th>NIM</th>
  80.                                 <td>: <input type="number" name="nim" placeholder="nim.." required></td>
  81.                             </tr>
  82.                             <tr>
  83.                                 <th>Nama Lengkap</th>
  84.                                 <td>: <input type="text" name="nama" placeholder="Nama Lengkap.." required></td>
  85.                             </tr>
  86.                             <tr>
  87.                                 <th>Alamat</th>
  88.                                 <td>: <input type="text" name="alamat" placeholder="Alamat.." required></td>
  89.                             </tr>
  90.                             <tr>
  91.                                 <th>Jurusan</th>
  92.                                 <td>: <input type="text" name="jurusan" placeholder="Jurusan.." required></td>
  93.                             </tr>
  94.                             <tr>
  95.                                 <td><button type="submit" name="submit">Simpan</button></td>
  96.                             </tr>
  97.                         </table>
  98.                     </form>
  99.         <?php
  100.                 }else if($_GET['mode'] == 'edit'){
  101.                     if(isset($_GET['nim'])){
  102.                         if($data = $crud->read_data(trim($_GET['nim']))){
  103.                             if(isset($_POST['submit'])){
  104.                                 $crud->update_data(trim($_POST['nim']), trim($_POST['nama']), trim($_POST['alamat']), trim($_POST['jurusan']));
  105.                                 echo $crud->message == 1 ? "<script>alert('sukses update data')</script>" : "<script>alert('gagal update data')</script>";
  106.                                 echo "<meta http-equiv='refresh' content='0;url=./?mode=edit&nim=".$_POST['nim']."'>";
  107.                             }
  108.         ?>
  109.                             <h3>Edit Data | <a href="./">Home</a></h3>
  110.                             <form method="POST" action="./?mode=edit&nim=<?= $data['nim']; ?>">
  111.                                 <table>
  112.                                     <tr>
  113.                                         <th>NIM</th>
  114.                                         <td>: <input type="number" name="nim" placeholder="nim.." value="<?= $data['nim']; ?>" readonly></td>
  115.                                     </tr>
  116.                                     <tr>
  117.                                         <th>Nama Lengkap</th>
  118.                                         <td>: <input type="text" name="nama" placeholder="Nama Lengkap.." value="<?= $data['nama_lengkap']; ?>" required></td>
  119.                                     </tr>
  120.                                     <tr>
  121.                                         <th>Alamat</th>
  122.                                         <td>: <input type="text" name="alamat" placeholder="Alamat.." value="<?= $data['alamat']; ?>" required></td>
  123.                                     </tr>
  124.                                     <tr>
  125.                                         <th>Jurusan</th>
  126.                                         <td>: <input type="text" name="jurusan" placeholder="Jurusan.." value="<?= $data['jurusan']; ?>" required></td>
  127.                                     </tr>
  128.                                     <tr>
  129.                                         <td><button type="submit" name="submit">Update data</button></td>
  130.                                     </tr>
  131.                                 </table>
  132.                             </form>
  133.         <?php
  134.                         }else{
  135.                             echo "data not found <a href='./'>back to home</a>";
  136.                         }
  137.                     }
  138.                 }else if($_GET['mode'] == 'hapus'){
  139.                     if(isset($_GET['nim'])){
  140.                         if($crud->read_data(trim($_GET['nim']))){
  141.                             $crud->delete_data(trim($_GET['nim']));
  142.                             echo $crud->message == 1 ? "<script>alert('sukses hapus data')</script>" : "<script>alert('gagal hapus data')</script>";
  143.                             echo "<meta http-equiv='refresh' content='0;url=./'>";
  144.                         }else{
  145.                             echo "data not found <a href='./'>back to home</a>";
  146.                         }
  147.                     }
  148.                 }
  149.             } else {
  150.         ?>
  151.                 <h3>Home | <a href="./?mode=tambah">Tambah Data</a></h3>
  152.                 <table border="1">
  153.                     <thead>
  154.                         <tr>
  155.                             <th>No.</th>
  156.                             <th>NIM</th>
  157.                             <th>Nama Lengkap</th>
  158.                             <th>Alamat</th>
  159.                             <th>Jurusan</th>
  160.                             <th>Aksi</th>
  161.                         </tr>
  162.                     </thead>
  163.                     <tbody>
  164.                         <?php foreach($crud->read_data() as $data) { ?>
  165.                             <tr>
  166.                                 <td><?= $i++; ?></td>
  167.                                 <td><?= $data['nim']; ?></td>
  168.                                 <td><?= $data['nama_lengkap']; ?></td>
  169.                                 <td><?= $data['alamat']; ?></td>
  170.                                 <td><?= $data['jurusan']; ?></td>
  171.                                 <td><a href="./?mode=edit&nim=<?= $data['nim']; ?>">EDIT</a> | <a href="./?mode=hapus&nim=<?= $data['nim']; ?>" onclick="return confirm('Yakin mau hapus data ini?');">HAPUS</a></td>
  172.                             </tr>
  173.                         <?php } ?>
  174.                     </tbody>
  175.                 </table>
  176.         <?php
  177.             }
  178.         ?>
  179.     </body>
  180. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement