Advertisement
madeslimacek6

CURD (DELETE) master_user4.php

Jan 7th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.42 KB | None | 0 0
  1. <?php
  2. $a = !empty($_GET['a']) ? $_GET['a'] : "reset";
  3. $iduser = !empty($_GET['id']) ? $_GET['id'] : " ";
  4. $kdb = koneksidatabase();
  5. $a = @$_GET["a"];
  6. $sql = @$_POST["sql"];
  7. switch ($sql) {
  8.     case "insert": sql_insert(); break;
  9.     case "update": sql_update(); break;
  10.     case "delete": sql_delete(); break;
  11. }
  12.  
  13. switch ($a) {
  14.     case "reset" :  curd_read();   break;
  15.     case "tambah":  curd_create(); break;
  16.     case "edit"  : curd_update($iduser); break;  
  17.     case "hapus" : curd_delete($iduser); break;
  18.     default : curd_read(); break;
  19. }
  20.   mysqli_close($kdb);
  21.  
  22. function curd_read()
  23. {
  24.   $hasil = sql_select();
  25.   $i=1;
  26.   ?>
  27.   <H3> MASTER DATA USER AKUN </H3>
  28.   <a href="master_user4.php?a=tambah" >CREATE</a>
  29.   <table border="1" cellpadding="10" cellspacing="0" >
  30.   <tr>
  31.   <td>No</td>
  32.   <td>ID</td>
  33.   <td>Username</td>
  34.   <td>Password</td>
  35.   <td>ID Mahasiswa</td>
  36.   <td>Publish</td>
  37.   <td>Aksi</td>
  38.   </tr>
  39.   <?php
  40.   while($baris = mysqli_fetch_array($hasil))
  41.   {
  42.   ?>
  43.   <tr>
  44.   <td><?= $i; ?></td>
  45.   <td><?= $baris['iduser']; ?></td>
  46.   <td><?= $baris['nmuser']; ?></td>
  47.   <td><?= $baris['password']; ?></td>
  48.   <td><?= $baris['idmahasiswa']; ?></td>
  49.   <td><?= $baris['publish']; ?></td>
  50.   <td>
  51.   <a href="master_user4.php?a=edit&id=<?php echo $baris['iduser']; ?>">UPDATE</a>  
  52.   <a href="master_user4.php?a=hapus&id=<?php echo $baris['iduser']; ?>">DELETE</a> 
  53.   </td>
  54.   </tr>
  55.   <?php
  56.    $i++;  
  57.   }
  58.   ?>
  59.   </table>  
  60.    <?php
  61.   mysqli_free_result($hasil);
  62. }
  63.  ?>
  64.  
  65.  
  66. <?php
  67. function formeditor($row)
  68.   {
  69. ?>
  70. <table>
  71. <tr>
  72. <td width="200px">Nama User</td>
  73. <td><input type="text" name="nmuser" id="nmuser" maxlength="25" size="25" value="<?php  echo trim($row["nmuser"]) ?>" ></td>
  74. </tr>
  75.  
  76. <tr>
  77. <td width="200px">Password</td>
  78. <td><input type="text" name="password" id="password" maxlength="25" size="25" value="<?php  echo trim($row["password"]) ?>" ></td>
  79. </tr>
  80.  
  81. <tr>
  82. <td width="200px">ID Mahasiswa</td>
  83. <td><input type="text" name="idmahasiswa" id="idmahasiswa" maxlength="25" size="25" value="<?php  echo trim($row["idmahasiswa"]) ?>" ></td>
  84. </tr>
  85.  
  86. <tr>
  87. <td >PUBLIKASI</td>
  88. <td >
  89. <?php  $publish = str_replace('"', '"', trim($row["publish"])); ?>
  90. <input type="radio" name="publish" id="publish" value="T" <?php  if($publish=='T' || $publish=='') {echo "checked=\"checked\""; } else {echo ""; }  ?> />
  91. <label>Dipublikasikan</label><br>
  92. <input type="radio" name="publish" id="publish" value="F" <?php  if($publish=='F') {echo "checked=\"checked\""; } else {echo ""; } ?> />
  93. <label>Tidak dipublikasikan</label>
  94. </td>
  95. </tr>
  96. </table>
  97. <?php  }?>
  98.  
  99. <?php
  100. function curd_create()
  101. {
  102. ?>
  103. <h3>Penambahan Data User</h3><br>
  104. <a href="master_user4.php?a=reset">Batal</a>
  105. <br>
  106. <form action="master_user4.php?a=reset" method="post">
  107. <input type="hidden" name="sql" value="insert" >
  108. <?php
  109. $row = array(
  110.   "nmuser" => "",
  111.   "password" => "",
  112.   "idmahasiswa" => "",
  113.   "publish" => "T");
  114. formeditor($row)
  115. ?>
  116. <p><input type="submit" name="action" value="Simpan" ></p>
  117. </form>
  118. <?php } ?>
  119.  
  120. <?php
  121. function curd_update($iduser)
  122. {
  123.     global $kdb;
  124.     $hasil2 = sql_select_byid($iduser);
  125.     $row = mysqli_fetch_array($hasil2);
  126.     ?>
  127.     <h3>Pengubahan Data User</h3><br>
  128.     <a href="master_user4.php?a=reset">Batal</a>
  129.     <br>
  130.     <form action="master_user4.php?a=reset" method="post">
  131.     <input type="hidden" name="sql" value="update" >
  132.     <input type="hidden" name="iduserx" value="<?php  echo $iduser; ?>" >
  133.     <?php
  134.     formeditor($row)
  135.     ?>
  136.     <p><input type="submit" name="action" value="Update" ></p>
  137.     </form>
  138.     <?php } ?>
  139.  
  140. <?php
  141. function curd_delete($iduser)
  142. {
  143. global $kdb;
  144. $hasil2 = sql_select_byid($iduser);
  145. $row = mysqli_fetch_array($hasil2);
  146. ?>
  147. <h3>Penghapusan Data User</h3><br>
  148. <a href="master_user4.php?a=reset">Batal</a>
  149. <br>
  150. <form action="master_user4.php?a=reset" method="post">
  151. <input type="hidden" name="sql" value="delete" >
  152. <input type="hidden" name="iduserx" value="<?php  echo $iduser; ?>" >
  153. <h3> Anda yakin akan menghapus data User <?php echo $row['nmuser'];?> </h3>
  154. <p><input type="submit" name="action" value="Delete" ></p>
  155. </form>
  156. <?php } ?>
  157.  
  158. <?php
  159. function koneksidatabase()
  160. {
  161.     include('../koneksi/koneksi.php');
  162.     return $kdb;
  163. }
  164.  
  165. function sql_select()
  166. {
  167.   global $kdb;
  168.   $sql = " select * from user ";
  169.   $hasil = mysqli_query($kdb, $sql) or die(mysql_error());
  170.   return $hasil;
  171. }
  172.  
  173. function sql_insert()
  174. {
  175.   global $kdb;
  176.   global $_POST;
  177.   echo $_POST["nmuser"];
  178.   $sql  = " insert into `user` (`nmuser`, `password`, `idmahasiswa`, `publish`) values ( '".$_POST["nmuser"]."', '".$_POST["password"]."', '".$_POST["idmahasiswa"]."', '".$_POST["publish"]."' )";          
  179.   mysqli_query($kdb, $sql) or die( mysql_error());
  180. }
  181.  
  182. function sql_select_byid($iduser)
  183. {
  184.     global $kdb;
  185.     $sql = " select * from user where iduser = ".$iduser;
  186.     $hasil2 = mysqli_query($kdb, $sql) or die(mysql_error());
  187.     return $hasil2;
  188.   }
  189.  
  190.   function sql_update()
  191.   {
  192.       global $kdb;
  193.       global $_POST;
  194.       $sql  = " update  `user` set `nmuser` = '".$_POST["nmuser"]."', password = '".$_POST["password"]."', publish = '".$_POST["publish"]."' where iduser = ".$_POST["iduserx"];             
  195.       mysqli_query($kdb, $sql) or die( mysql_error());
  196.   }
  197.  
  198. function sql_delete()
  199. {
  200.   global $kdb;
  201.   global $_POST;
  202.   $sql  = " delete from `user` where iduser = ".$_POST["iduserx"];           
  203.   mysqli_query($kdb, $sql) or die( mysql_error());
  204. }
  205.  
  206.  
  207. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement