Advertisement
Guest User

CRUD Simple

a guest
Apr 1st, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.26 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. <style type="text/css">
  7. <!--
  8. #form1 table tr td {
  9.     text-align: center;
  10. }
  11. -->
  12. </style>
  13. </head>
  14. <?PHP
  15.  
  16. include('koneksi.php');
  17.  
  18. //SCRIPT untuk save dan simpan baru
  19.     if($_POST){
  20.         $id       = $_POST['id'] ;
  21.         $username = $_POST['username'];
  22.         $password = $_POST['password'];
  23.         if (empty($username) || empty($password)){
  24.        
  25.             echo"Username atau Password tidak boleh kosong";
  26.        
  27.         }else{
  28.            
  29.             //untuk ngecek kalo variabel idnya ada perintah sql yang di jalannin sql untuk edit kalo variabel idnya kosong perintah yang di jalanin sql untuk save
  30.             if(!empty($id)){
  31.                 $simpan = mysql_query("UPDATE  user SET  username =  '$username', password =  '$password' WHERE  id = '$id';");
  32.             }else{ 
  33.                 $simpan = mysql_query("INSERT INTO user (`id`, `username`, `password`) VALUES ('NULL', '$username', '$password')");
  34.             }
  35.  
  36.             if($simpan){
  37.                 echo"<script>alert('Data tersimpan!');document.location.href='login.php';</script>";
  38.             }
  39.             else{
  40.                 echo"<script>alert('Data gagal tersimpan!');document.location.href='login.php';</script>";
  41.             }
  42.            
  43.         }      
  44.        
  45.     }
  46. //Kalo get edit, mengambil data dan sebagainya dari database
  47.     if(isset($_GET['edit'])){
  48.         $sql = "SELECT * FROM  user WHERE id = '$_GET[id]'";
  49.         $query = mysql_query($sql);
  50.         $r = mysql_fetch_array($query);
  51.     }
  52. //SCRIPT HAPUS
  53.     if(isset($_GET['hapus'])){
  54.         $hapus = mysql_query("DELETE FROM user where id = '$_GET[id]'");
  55.         if($hapus){
  56.             echo"Data Terhapus!";
  57.         }
  58.         else{
  59.             echo"Data Gagal Terhapus!";
  60.         }
  61.     }
  62.  
  63.  
  64.  
  65. ?>
  66.  
  67.  
  68. <body>
  69. <form id="form1" name="form1" method="post" action="">
  70.     <input type="hidden" name="id" value="<?php echo (isset($r))? $r['id'] : '';?>">
  71.   <table width="270" border="0" cellspacing="1" cellpadding="2">
  72.     <tr>
  73.       <td colspan="2">FORM USER</td>
  74.     </tr>
  75.     <tr>
  76.       <td width="96">Username</td>
  77.       <td width="163"><label>
  78.         <input type="text" name="username" id="textfield"  value="<?php echo (isset($r))? $r['username'] : '';?>"/>
  79.       </label></td>
  80.     </tr>
  81.     <tr>
  82.       <td>Password</td>
  83.       <td><label>
  84.         <input type="text" name="password" id="textfield2"  value="<?php echo (isset($r))? $r['password'] : '';?>"/>
  85.       </label></td>
  86.     </tr>
  87.     <tr>
  88.       <td colspan="2"><label>
  89.         <input type="submit" name="button" id="button" value="Submit" />
  90.         <input type="reset" name="button2" id="button2" value="Cancel" />
  91.       </label></td>
  92.     </tr>
  93.   </table>
  94. </form>
  95. <table>
  96.     <tr>
  97.         <td>No</td>
  98.         <td>Usenname</td>
  99.         <td>Password</td>
  100.     </tr>
  101.     <?php
  102.         $sql = "SELECT * FROM user ";
  103.         $query = mysql_query($sql);
  104.         $no = 0;
  105.         while($row = mysql_fetch_array($query)){
  106.             $no++;
  107.     ?>
  108.     <tr>
  109.         <td><?php echo $no; ?></td>
  110.         <td><?php echo $row['1'];?></td>
  111.         <td><?php echo $row['password']; ?></td>
  112.         <td><a href="?edit&id=<?php echo $row['id']; ?>">Edit</a> | <a href="?hapus&id=<?php echo $row['id']; ?>">Delete</a></td>
  113.     </tr>
  114.     <?php
  115.         }
  116.     ?>
  117. </table>
  118.  
  119.  
  120.  
  121.  
  122.  
  123. </body>
  124. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement