Advertisement
haikelfazzani

How To Insert Data Into MySql Using Php

Jul 23rd, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.06 KB | None | 0 0
  1. <?php
  2.     require_once("connection.php");
  3.    
  4.     if($_SERVER['REQUEST_METHOD'] == 'POST'){
  5.        
  6.         $fullName = test_input($_POST['fullname']);
  7.         $Email = test_input($_POST['email']);
  8.         $userName = test_input($_POST['username']);
  9.         $passWord = test_input($_POST['password']);
  10.        
  11.         if(!empty($fullName) AND !empty($Email) AND !empty($userName) AND !empty($passWord)){
  12.            
  13.             if(filter_var($Email,FILTER_VALIDATE_EMAIL)){ // check if the email is valid or no
  14.                
  15.                 if (preg_match('/^[0-9a-zA-Z ]+$/', $fullName)){ // the full name can contain only numbers ,letters and white space
  16.                    
  17.                     $request = 'INSERT INTO users(fullname,email,username,password)
  18.                     VALUES(:fullname,:email,:username,:password)';
  19.            
  20.                     $stmt = $connect->prepare($request);
  21.            
  22.                     $stmt->bindValue(':fullname',$fullName,PDO::PARAM_STR);
  23.                     $stmt->bindValue(':email',$Email,PDO::PARAM_STR);
  24.                     $stmt->bindValue(':username',$userName,PDO::PARAM_STR);
  25.                     $stmt->bindValue(':password',$passWord,PDO::PARAM_STR);
  26.                    
  27.                     $stmt->execute();
  28.                    
  29.                     echo 'new record add to database';
  30.                    
  31.                 }else{
  32.                    
  33.                     echo 'Unvalid Full Name ; Only Numbers ,Letters And White Space';
  34.                      exit();
  35.                 }
  36.             }else{
  37.                
  38.                 echo 'Unvalid Email';
  39.                 exit();
  40.                
  41.             }
  42.         }else{
  43.            
  44.             echo 'Unvalid Input';
  45.              exit();
  46.         }
  47.     }
  48.    
  49.     function test_input($inputField){
  50.        
  51.         $inputField = htmlspecialchars(strip_tags(stripcslashes($inputField)));
  52.         return $inputField;
  53.        
  54.     }
  55. $connect = NULL;
  56. ?>
  57.  
  58. <!DOCTYPE html>
  59. <html lang="en">
  60. <head>
  61.   <title>codeJs</title>
  62.   <meta charset="utf-8">
  63.   <meta name="viewport" content="width=device-width, initial-scale=1">
  64.   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  65. </head>
  66. <body>
  67.  
  68. <div class="container">
  69.     <h2>Code Js : Register Now</h2>
  70.         <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
  71.        
  72.             <div class="form-group">
  73.                 <label for="fullname">Full Name :</label>
  74.                 <input type="text" class="form-control" placeholder="Your Full Name" name="fullname">
  75.             </div>
  76.            
  77.             <div class="form-group">
  78.                 <label for="email">Email :</label>
  79.                 <input type="text" class="form-control" placeholder="Your Email" name="email">
  80.             </div>
  81.            
  82.             <div class="form-group">
  83.                 <label for="username">Username :</label>
  84.                 <input type="text" class="form-control" placeholder="Your Username" name="username">
  85.             </div>
  86.            
  87.             <div class="form-group">
  88.                 <label for="password">Password:</label>
  89.                 <input type="text" class="form-control" placeholder="Your Password" name="password">
  90.             </div>
  91.        
  92.             <button type="submit" class="btn btn-primary">Register</button>
  93.             <a href='login.php' class="btn btn-warning">Log In</a>
  94.         </form>
  95. </div>
  96.  
  97.  
  98. <style>form, h2{ width: 400px; margin: 0 auto; }
  99. h2{ margin-top: 50px; margin-bottom: 30px; color: #ea4444; }
  100. </style>
  101. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  102. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  103. </body>
  104. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement