Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.39 KB | None | 0 0
  1. <?php
  2. $name_from_form=$lastname_from_form=$username_from_form=$password_from_form=$rpassword_from_form=$email_from_form=$am_from_form=$semester_from_form=$telephone_from_form="";
  3.  
  4. $no_error=true;
  5. Validations();
  6.  
  7. if($no_error==true){
  8.     $con = mysqli_connect('localhost','root');
  9.     if (!$con) {
  10.     die('Could not connect ERROR: ' . mysqli_error());
  11.     }
  12. else{        
  13.         insertStudent();
  14.     }
  15. }
  16.  
  17.  
  18.  
  19. function Validations(){
  20.     nameValid();
  21.     lastnameValid();
  22.     passwordValid();
  23.     rpasswordValid();
  24.     semesterValid();
  25.     amValid();
  26.     telephoneValid();
  27.     emailValid();
  28. }
  29.  
  30. function test_input($data) {
  31.   $data = trim($data);
  32.   $data = stripslashes($data);
  33.   $data = htmlspecialchars($data);
  34.   return $data;
  35. }
  36.  
  37. function nameValid(){
  38.     if (empty($_POST["name"])) {
  39.     echo"Name field is empty";echo "<br>";
  40.     $no_error=false;
  41.   } else {
  42.     $name_from_form = test_input($_POST["name"]);
  43.     $name_length=strlen($name_from_form);
  44.     if (!preg_match("/^[a-zA-Z ]*$/",$name_from_form)) {
  45.       echo"Name Error";echo "<br>";
  46.       $no_error=false;
  47.     }
  48.     if($name_length>25 || $name_length<5){
  49.         echo"Name length error";
  50.         $no_error=false;
  51.     }
  52.   }
  53. }
  54. function lastnameValid(){
  55.     if (empty($_POST["lastname"])) {
  56.     echo"Lastname field is empty";echo "<br>";
  57.     $no_error=false;
  58.   } else {
  59.     $lastname_from_form = test_input($_POST["lastname"]);
  60.     $lastname_length=strlen($lastname_from_form);
  61.     if (!preg_match("/^[a-zA-Z ]*$/",$lastname_from_form)) {
  62.       echo"Lastname Error";echo "<br>";
  63.       $no_error=false;
  64.     }
  65.     if($lastname_length>25 || $lastname_length<5){
  66.         echo"Lastname length error";
  67.         $no_error=false;
  68.     }
  69.   }
  70. }
  71. function usernameValid(){
  72.     if (empty($_POST["username"])) {
  73.     echo"Username field is empty";echo "<br>";
  74.     $no_error=false;
  75.   } else {
  76.     $username_from_form = test_input($_POST["username"]);
  77.     $username_length=$strlen($username_from_form);
  78.     if (!preg_match("/^[a-zA-Z ]*$/",$username_from_form)) {
  79.       echo"Username Error";echo "<br>";
  80.       $no_error=false;
  81.     }
  82.     if($username_length>25 || $username_length<8){
  83.         echo"Username length error";
  84.         $no_error=false;
  85.     }
  86.   }
  87. }
  88.  
  89. function passwordValid(){
  90.     if (empty($_POST["password"])) {
  91.     echo"Password field is empty";echo "<br>";
  92.     $no_error=false;
  93.   } else {
  94.     $password_from_form = test_input($_POST["password"]);
  95.     $pass_length=strlen($password_from_form);
  96.     if ($pass_length>15 || $pass_length<8) {
  97.       echo"Password length error";echo "<br>";
  98.       $no_error=false;
  99.     }
  100.   }
  101. }
  102. function semesterValid(){
  103.     if (empty($_POST["semester"])) {
  104.     echo"Semester field is empty";echo "<br>";
  105.     $no_error=false;
  106.   } else {
  107.     $semester_from_form = test_input($_POST["semester"]);
  108.     $semester_length=strlen($semester_from_form);
  109.     if (!preg_match("/^[0-9]+$/",$semester_from_form)) {
  110.       echo"Semester Error";echo "<br>";
  111.       $no_error=false;
  112.     }
  113.     if ($semester_length>2) {
  114.       echo"Semester length error";echo "<br>";
  115.       $no_error=false;
  116.     }
  117.   }
  118. }
  119. function amValid(){
  120.     if (empty($_POST["am"])) {
  121.     echo"ΑΜ field is empty";echo "<br>";
  122.     $no_error=false;
  123.   } else {
  124.     $am_from_form = test_input($_POST["am"]);
  125.     $am_length=strlen($am_from_form);
  126.     if ($am_length>10) {
  127.       echo"AM length error";echo "<br>";
  128.       $no_error=false;
  129.     }
  130.   }
  131. }
  132. function telephoneValid(){
  133.     if (empty($_POST["telephone"])) {
  134.     echo"Telephone field is empty";echo "<br>";
  135.     $no_error=false;
  136.   } else {
  137.     $telephone_from_form = test_input($_POST["telephone"]);
  138.     $telephone_length=strlen($telephone_from_form);
  139.     if ($telephone_length<10 || $telephone_length>15) {
  140.       echo"Telephone length error";echo "<br>";
  141.       $no_error=false;
  142.     }
  143.   }
  144. }
  145. function rpasswordValid(){
  146.     if (empty($_POST["rpassword"])) {
  147.     echo"Confirm password field is empty";echo "<br>";
  148.     $no_error=false;
  149.   } else {
  150.     $rpassword_from_form = test_input($_POST["rpassword"]);
  151.     $pass=test_input($_POST["password"]);
  152.     if (strcmp($rpassword_from_form,$pass) !=0) {
  153.         echo"Confirm password does not match the password";echo "<br>";
  154.         $no_error=false;
  155.     }
  156.   }
  157. }
  158. function emailValid(){
  159.     if (empty($_POST["email"])) {
  160.     echo"Email field is empty";echo "<br>";
  161.     $no_error=false;
  162.   } else {
  163.     $email_from_form = test_input($_POST["email"]);
  164.     if (!filter_var($email_from_form, FILTER_VALIDATE_EMAIL)) {
  165.       echo"Invalid mail";echo "<br>";
  166.       $no_error=false;
  167.     }
  168.   }
  169. }
  170. function insertStudent(){
  171.         $name_from_form = test_input($_POST["name"]);
  172.         $lastname_from_form = test_input($_POST["lastname"]);
  173.         $username_from_form = test_input($_POST["username"]);
  174.         $password_from_form = test_input($_POST["password"]);
  175.         $semester_from_form = test_input($_POST["semester"]);
  176.         $am_from_form = test_input($_POST["am"]);
  177.         $telephone_from_form = test_input($_POST["telephone"]);
  178.         $email_from_form = test_input($_POST["email"]);
  179.         $shauser = sha1($username_from_form);
  180.         $shapass =  sha1($password_from_form);
  181.         $con = mysqli_connect('localhost','root');
  182.         mysqli_select_db($con,"education");
  183.         $insert_query="INSERT INTO STUDENT (username,password,am,name,surname,semester,telephone,email)VALUES
  184.                   ('$shauser','$shapass','$am_from_form','$name_from_form','$lastname_from_form','$semester_from_form','$telephone_from_form','$email_from_form')";        
  185.                     if (mysqli_query($con, $insert_query)) {
  186.         echo "New record created successfully";
  187. }       else {
  188.         echo "Error: " . $insert_query . "<br>" . mysqli_error($con);
  189. }
  190.                     mysqli_close($con);
  191.    
  192. }
  193.  
  194. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement