SHARE
TWEET

crud-oop

a guest Mar 20th, 2016 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // edit.php
  2.  
  3. <?php
  4. include_once 'class.php';
  5. $db = new Connections(); // instansiasi obj
  6. $db->OpenConnection(); // get connection
  7. $getData = new dataDiri();
  8. $id = isset( $_GET['id_usr'] ) ? mysql_real_escape_string( $_GET['id_usr'] ) : NULL;
  9. ?>
  10. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  11.     <table>
  12.         <tr>
  13.             <td>Nama</td>
  14.             <td>:</td>
  15.             <td><input type="text" value="<?php echo$getData->edit_data( 'nama', $id ); ?>"></td>
  16.         </tr>
  17.         <tr>
  18.             <td>Email</td>
  19.             <td>:</td>
  20.             <td><input type="text" value="<?php echo$getData->edit_data( 'email', $id ); ?>"></td>
  21.         </tr>
  22.         <tr>
  23.             <td>No. Telepon</td>
  24.             <td>:</td>
  25.             <td><input type="text" value="<?php echo$getData->edit_data( 'telp', $id ); ?>"></td>
  26.         </tr>
  27.         <tr>
  28.             <td>Username</td>
  29.             <td>:</td>
  30.             <td><input type="text" value="<?php echo$getData->edit_data( 'username', $id ); ?>"></td>
  31.         </tr>
  32.         <tr>
  33.             <td>Password</td>
  34.             <td>:</td>
  35.             <td><input type="text" value="<?php echo$getData->edit_data( 'password', $id ); ?>"></td>
  36.         </tr>
  37.         <tr>
  38.             <td></td>
  39.             <td></td>
  40.             <td></td>
  41.         </tr>
  42.     </table>
  43. </form>
  44.  
  45. <?php
  46. // UPDATE DATA
  47. // $getData->update_data( $_POST['nama'], );
  48. ?>
  49.  
  50.  
  51. // class.php
  52.  
  53. <?php
  54.  
  55. class Connections {
  56.     private $conn;
  57.     private $db_host = 'localhost';
  58.     private $db_user = 'root';
  59.     private $db_pswd = '';
  60.     private $db_name = 'lat_crud';
  61.  
  62.     function OpenConnection() {
  63.         $this->conn = mysql_connect( $this->db_host, $this->db_user, $this->db_pswd ) or die ( "fuck". mysql_error() );
  64.         if ( $this->conn ) {
  65.             mysql_select_db( $this->db_name );
  66.         } else {
  67.             die( "Connection problem !". mysql_error() );
  68.         }
  69.     }
  70. } // END: Class Connections
  71.  
  72.  
  73. class DataDiri {
  74.     function insert_data( $nama, $email, $telp, $username, $password ) {
  75.         $sql = "INSERT INTO data_diri( nama, email, telp, username, password )
  76.                 VALUES ( '$nama', '$email', '$telp', '$username', '$password' )";
  77.         $query = mysql_query( $sql );
  78.         return $query;
  79.     }
  80.  
  81.     function display_data() {
  82.         $data = '';
  83.         $sql = mysql_query( "SELECT * FROM data_diri" );
  84.         while ( $row = mysql_fetch_assoc( $sql ) ) {
  85.             $data[] = $row;
  86.         }
  87.         return $data;
  88.     }
  89.  
  90.     function edit_data( $field, $id_usr ) {
  91.         $sql = mysql_query( "SELECT * FROM data_diri WHERE id_user = '$id_usr'" );
  92.         $data = mysql_fetch_assoc( $sql );
  93.         if ( $field == 'nama' ) return $data['nama'];
  94.         elseif ( $field == 'email ') return $data['email'];
  95.         elseif ( $field == 'telp ') return $data['telp'];
  96.         elseif ( $field == 'username ') return $data['username'];
  97.         elseif ( $field == 'password ') return $data['password'];
  98.     }
  99.  
  100.     function update_data() {
  101.  
  102.     }
  103.  
  104.     function delete_data( $id ) {
  105.         $sql = mysql_query( "DELETE FROM data_diri WHERE id_user = '$id'" );
  106.         return $sql;
  107.     }
  108.  
  109. } // END: Class DataDiri
  110.  
  111. ?>
  112.  
  113.  
  114.  
  115.  
  116.  
  117. // index.php
  118.  
  119. <?php  
  120. include_once 'class.php';
  121.  
  122. $db = new Connections(); // instansiasi obj
  123. $db->OpenConnection(); // get connection
  124. ?>
  125.  
  126. <form action="" method="post">
  127.     <table>
  128.         <tr>
  129.             <td></td>
  130.             <td></td>
  131.             <td><input type="text" name="nama" placeholder="Nama"></td>
  132.         </tr>
  133.         <tr>
  134.             <td></td>
  135.             <td></td>
  136.             <td><input type="email" name="email" placeholder="Email"></td>
  137.         </tr>
  138.         <tr>
  139.             <td></td>
  140.             <td></td>
  141.             <td><input type="text" name="telp" placeholder="No Telp"></td>
  142.         </tr>
  143.         <tr>
  144.             <td></td>
  145.             <td></td>
  146.             <td><input type="text" name="username" placeholder="Username"></td>
  147.         </tr>
  148.         <tr>
  149.             <td></td>
  150.             <td></td>
  151.             <td><input type="password" name="password" placeholder="Password"></td>
  152.         </tr>
  153.         <tr>
  154.             <td></td>
  155.             <td></td>
  156.             <td><input type="submit" name="btn-save" value="save"></td>
  157.         </tr>
  158.     </table>
  159. </form>
  160.  
  161. <br>
  162. <table>
  163.     <tr>
  164.         <td>No</td>
  165.         <td>Nama</td>
  166.         <td>Email</td>
  167.         <td>No. Telepon</td>
  168.         <td>Username</td>
  169.         <td>Password</td>
  170.         <td colspan="2">Aksi</td>
  171.     </tr>
  172.     <?php  
  173.     $dt = new DataDiri();
  174.     $display_data = $dt->display_data();
  175.     $no = 1;
  176.     if ( ! empty( $display_data ) ) :
  177.         foreach ( $display_data as $row ) :
  178.     ?>
  179.             <tr>
  180.                 <td><?= $no; ?></td>
  181.                 <td><?= $row['nama']; ?></td>
  182.                 <td><?= $row['email']; ?></td>
  183.                 <td><?= $row['telp']; ?></td>
  184.                 <td><?= $row['username']; ?></td>
  185.                 <td><?= '**********'; ?></td>
  186.                 <td><a href="edit-data.php?id_usr=<?php echo $row['id_user']; ?>">Edit</a></td>
  187.                 <td><a href="">Hapus</a></td>
  188.             </tr>
  189.             <?php  $no++;
  190.         endforeach;
  191.     else :
  192.         echo 'Tidak ada data.';
  193.     endif;
  194.     ?>
  195. </table>
  196.  
  197. <?php
  198. if ( isset( $_POST['btn-save'] ) ) {
  199.     $nama = $_POST['nama'];
  200.     $email = $_POST['email'];
  201.     $telp = $_POST['telp'];
  202.     $username = $_POST['username'];
  203.     $password = md5( $_POST['password'] );
  204.  
  205.     if ( empty( $nama ) || empty( $email ) || empty( $telp ) || empty( $username ) || empty( $password ) ) {
  206.         echo "<script>alert( 'Data tidak boleh kosong !' ); window.location.href='./';</script>";
  207.     } else {
  208.         $insert = new DataDiri(); // instansiasi
  209.         $insert->insert_data( $nama, $email, $telp, $username, $password );
  210.  
  211.         echo "<script>alert( 'Data berhasil disimpan !' ); window.location.href='./';</script>";
  212.     }
  213. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top