Advertisement
Guest User

Untitled

a guest
Apr 15th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.99 KB | None | 0 0
  1. <?php
  2. error_reporting(~E_NOTICE ); // avoid notice
  3. ini_set('display_errors',1);
  4.  
  5. require_once 'koneksi.php';
  6. if(isset($_POST['submit'])){
  7.     // Simpan data yang di inputkan ke POST ke masing-masing variable
  8.     // dan convert semua tag HTML yang mungkin dimasukkan untuk mengindari XSS
  9.     $username = htmlentities($_POST['username']);
  10.     $password = htmlentities($_POST['password']);
  11.     $nama = htmlentities($_POST['nama']);
  12.     $jk = htmlentities($_POST['jk']);
  13.     $wni = htmlentities($_POST['wni']);
  14.     $level = htmlentities($_POST['level']);
  15.  
  16.     $imgFile = $_FILES['foto']['name'];
  17.         $tmp_dir = $_FILES['foto']['tmp_name'];
  18.         $imgSize = $_FILES['foto']['size'];
  19.     $alamat = htmlentities($_POST['alamat']);
  20.  
  21.     if(empty($username)){
  22.             $errMSG = "Please Enter Username.";
  23.         }
  24.         else if(empty($password)){
  25.             $errMSG = "Please Enter Your Job Work.";
  26.         }
  27.         else if(empty($imgFile)){
  28.             $errMSG = "Please Select Image File.";
  29.         }
  30.         else
  31.         {
  32.             $upload_dir = 'images/'; // upload directory
  33.  
  34.             $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
  35.  
  36.             // valid image extensions
  37.             $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
  38.  
  39.             // rename uploading image
  40.             $userpic = rand(1000,1000000).".".$imgExt;
  41.  
  42.             // allow valid image file formats
  43.             if(in_array($imgExt, $valid_extensions)){
  44.                 // Check file size '5MB'
  45.                 if($imgSize < 5000000)              {
  46.                     move_uploaded_file($tmp_dir,$upload_dir.$userpic);
  47.                 }
  48.                 else{
  49.                     $errMSG = "Sorry, your file is too large.";
  50.                 }
  51.             }
  52.             else{
  53.                 $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  54.             }
  55.         }
  56.  
  57.  
  58.         // if no error occured, continue ....
  59.         if(!isset($errMSG))
  60.         {
  61.                     $query = $koneksi->prepare('INSERT INTO login(`username`, `password`, `nama`, `jk`, `wni`, `level`,`foto`,`alamat`)');
  62.               $query->bindParam(":username", $username);
  63.               $query->bindParam(":password", $password);
  64.               $query->bindParam(":nama", $nama);
  65.               $query->bindParam(":jk", $jk);
  66.               $query->bindParam(":wni", $wni);
  67.               $query->bindParam(":level", $level);
  68.               $query->bindParam(":foto", $userPic);
  69.               $query->bindParam(":alamat", $alamat);
  70.  
  71.             if($query->execute())
  72.             {
  73.                 $successMSG = "new record succesfully inserted ...";
  74.                 header("refresh:5;index.php"); // redirects image view page after 5 seconds.
  75.         exit;
  76.       }
  77.             else
  78.             {
  79.                 $errMSG = "error while inserting....";
  80.             }
  81.         }
  82.     }
  83. ?>
  84.  
  85.  
  86. <!DOCTYPE html>
  87. <html>
  88.   <head>
  89.     <meta charset="utf-8">
  90.     <title>Form Daftar</title>
  91.   </head>
  92.   <body>
  93.     <h1>Form Daftar</h1>
  94.     <form method="post" enctype="multipart/form-data">
  95.       <h3>Username</h3>
  96.         <label for="username"></label>
  97.         <input type="text" name="username" value="" required="" placeholder="Username">
  98.       <h3>Password</h3>
  99.         <label for="password"></label>
  100.         <input type="password" name="password" value="" placeholder="Password" required="">
  101.       <h3>Nama</h3>
  102.         <label for="nama"></label>
  103.         <input type="text" name="nama" value="" required="" placeholder="Mis: Agus">
  104.       <h3>Jenis Kelamin</h3>
  105.         <input type="radio" name="jk" value="Pria">Pria
  106.         <input type="radio" name="jk" value="Wanita">Wanita
  107.       <h3>Warga Indonesia?</h3>
  108.         <input type="checkbox" name="wni" value="Iya">Iya
  109.         <input type="checkbox" name="wni" value="Tidak">Tidak
  110.       <h3>Level Admin</h3>
  111.         <label for="level"></label>
  112.         <select class="level" name="level" required>
  113.           <option value="Admin">Admin</option>
  114.           <option value="User">User</option>
  115.       </select>
  116.       <h3>Foto</h3>
  117.         <label for="foto"></label>
  118.         <input type="file" name="foto" accept="image/*">
  119.       <h3>Alamat</h3>
  120.         <textarea name="alamat" rows="8" cols="40"></textarea>
  121.       <br><br>
  122.       <input type="submit" name="submit" value="Daftar">
  123.     </form>
  124.   </body>
  125. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement