Advertisement
Guest User

oop-mysqli

a guest
Mar 13th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.63 KB | None | 0 0
  1. // START: File connection.php ==============================================
  2. <?php
  3. class Connections {
  4.  
  5.     var $conn;
  6.     private $db_host = 'localhost';
  7.     private $db_user = 'root';
  8.     private $db_pswd = '';
  9.     private $db_name = 'lat_crud';
  10.  
  11.     public function OpenConnection() {
  12.         $this->conn = new mysqli( $this->db_host, $this->db_user, $this->db_pswd, $this->db_name );
  13.         if ( $this->conn->connect_errno ) {
  14.             die ( "Connection problem !". $this->conn->connect_error );
  15.         }
  16.         // return $this->conn;
  17.     }
  18.  
  19.     public function CloseConnection() {
  20.         $this->conn->close();
  21.     }
  22. } // END: Class Connections
  23.  
  24. // END: File connection.php ==================================================
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34. // START: File data-diri.php =========================================================
  35. <?php
  36.  
  37. include "Connection.php";
  38.  
  39. class DataDiri {
  40.  
  41.     private $c, $nama, $email, $telp, $username, $password;
  42.     private $insert;
  43.  
  44.     public function getNama() {
  45.         return $this->nama;
  46.     }
  47.     public function setNama( $nama ) {
  48.         $this->nama = $nama;
  49.     }
  50.     public function getEmail() {
  51.         return $this->email;
  52.     }
  53.     public function setEmail( $email ) {
  54.         return $this->email = $email;
  55.     }
  56.     public function getTelp() {
  57.         return $this->telp;
  58.     }
  59.     public function setTelp( $telp ) {
  60.         $this->telp = $telp;
  61.     }
  62.     public function getUsername() {
  63.         return $this->username;
  64.     }
  65.     public function setUsername( $username ) {
  66.         $this->username = $username;
  67.     }
  68.     public function getPassword() {
  69.         return $this->password;
  70.     }
  71.     public function setPassword( $password ) {
  72.         return $this->password = $password;
  73.     }
  74.  
  75.     public function getInsert() {
  76.         $insert = false;
  77.        
  78.         $sql = "INSERT INTO data_diri( nama, email, telp, username, password )
  79.                 VALUES ( '". $this->getNama() ."',
  80.                     '". $this->getEmail() ."',
  81.                     '". $this->getTelp() ."',
  82.                     '". $this->getUsername() ."',
  83.                     '". md5( $this->getPassword() ) ."'
  84.                 )";
  85.                    
  86.         $c = new Connections();
  87.         $c->OpenConnection();
  88.         $query = $this->conn->query( $sql ) or die ( $this->conn->error );
  89.         if ( $query ) {
  90.             $insert = true;
  91.             return $insert;
  92.             $c->CloseConnection();
  93.         }
  94.     }
  95. } // END: Class DataDiri
  96.  
  97. // ================
  98. $nama = $_POST['nama'];
  99. $email = $_POST['email'];
  100. $telp = $_POST['telp'];
  101. $username = $_POST['username'];
  102. $password = $_POST['password'];
  103.  
  104. $dt = new DataDiri();
  105. $dt->setNama( $nama );
  106. $dt->setEmail( $email );
  107. $dt->setTelp( $telp );
  108. $dt->setUsername( $username );
  109. $dt->setPassword( $password );
  110. $hasil = $dt->getInsert();
  111.  
  112. if ( $hasil ) {
  113.     echo "<script>alert( 'Data berhasil disimpan !' ); window.location.href='./';</script>";
  114. } else {
  115.     die( 'Gagal tersimpan'. $this->conn->error );
  116. }
  117.  
  118. // END: File data-diri.php ==================================================
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. // START: File index.php ==================================================
  128. <form action="data-diri.php" method="post">
  129.     <table>
  130.         <tr>
  131.             <td></td>
  132.             <td></td>
  133.             <td><input type="text" name="nama" placeholder="Nama"></td>
  134.         </tr>
  135.         <tr>
  136.             <td></td>
  137.             <td></td>
  138.             <td><input type="email" name="email" placeholder="Email"></td>
  139.         </tr>
  140.         <tr>
  141.             <td></td>
  142.             <td></td>
  143.             <td><input type="text" name="telp" placeholder="No Telp"></td>
  144.         </tr>
  145.         <tr>
  146.             <td></td>
  147.             <td></td>
  148.             <td><input type="text" name="username" placeholder="Username"></td>
  149.         </tr>
  150.         <tr>
  151.             <td></td>
  152.             <td></td>
  153.             <td><input type="password" name="password" placeholder="Password"></td>
  154.         </tr>
  155.         <tr>
  156.             <td></td>
  157.             <td></td>
  158.             <td><input type="submit" name="submit-btn"></td>
  159.         </tr>
  160.     </table>
  161. </form>
  162.  
  163. // END: File index.php ==================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement