Advertisement
Guest User

simpan.php

a guest
Aug 22nd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.36 KB | None | 0 0
  1. /*
  2. ** percobaan pake db login
  3. ** tinggal penyesuaian aja
  4. **
  5. */
  6.  
  7. <?php
  8. if (isset($_POST['btn-simpan'])) {
  9.     $link = new mysqli('localhost', 'root', 'R0ckmyta', 'tutorial');
  10.     if ($link->connect_errno) {
  11.         echo "Can't Connect to Database : " . $link->connect_error;
  12.     }  
  13.    
  14.     $email  = strip_tags(trim($_POST['email']));
  15.     $user   = strip_tags(trim($_POST['nama']));
  16.     $pass   = strip_tags(trim($_POST['noreg']));
  17.    
  18.     // SQL Injection
  19.     $mail   = $link->real_escape_string($email);
  20.     $username=$link->real_escape_string($user);
  21.     $password=$link->real_escape_string($pass);
  22.    
  23.     // Cek Data
  24.     $stmt = $link->prepare("SELECT * FROM login WHERE email = ? OR username = ?");
  25.     $stmt->bind_param("ss", $email, $username);
  26.     $stmt->execute();
  27.    
  28.     // Ambil hasil query
  29.     $result = $stmt->get_result();
  30.    
  31.     // Cek email sudah pernah tersimpan di db / belum?
  32.     if ($result->num_rows > 0) {
  33.         echo "Sorry, username already exists";
  34.     } else {
  35.         // Insert Data
  36.         $insert = $link->prepare("INSERT INTO login (email, username, password) VALUES (?, ?, ?)");
  37.         $insert->bind_param("sss", $mail, $username, $password);
  38.         $insert->execute();
  39.        
  40.         if ($insert) {
  41.             echo "Success";
  42.             header("Location: index.php");
  43.         }
  44.     }
  45.  
  46. } /* end isset */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement