Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $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="";
- $no_errors=true;
- Validations();
- if($no_errors==true){
- $con = mysqli_connect('localhost','root');
- if (!$con) {
- die('Could not connect ERROR: ' . mysqli_error());}
- else{
- insertStudent();
- }
- }
- function Validations(){
- nameValid();
- lastnameValid();
- passwordValid();
- rpasswordValid();
- semesterValid();
- amValid();
- telephoneValid();
- emailValid();
- }
- function test_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- function nameValid(){
- if (empty($_POST["name"])) {
- echo"Name field is empty";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- } else {
- $name_from_form = test_input($_POST["name"]);
- $name_length=strlen($name_from_form);
- if (!preg_match("/^[a-zA-Z ]*$/",$name_from_form)) {
- echo"Name Error";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- }
- if($name_length>25 || $name_length<5){
- echo"Name length error";
- $_GLOBALS['$no_errors']=false;
- }
- }
- }
- function lastnameValid(){
- if (empty($_POST["lastname"])) {
- echo"Lastname field is empty";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- } else {
- $lastname_from_form = test_input($_POST["lastname"]);
- $lastname_length=strlen($lastname_from_form);
- if (!preg_match("/^[a-zA-Z ]*$/",$lastname_from_form)) {
- echo"Lastname Error";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- }
- if($lastname_length>25 || $lastname_length<5){
- echo"Lastname length error";
- $_GLOBALS['$no_errors']=false;
- }
- }
- }
- function usernameValid(){
- if (empty($_POST["username"])) {
- echo"Username field is empty";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- } else {
- $username_from_form = test_input($_POST["username"]);
- $username_length=$strlen($username_from_form);
- if (!preg_match("/^[a-zA-Z ]*$/",$username_from_form)) {
- echo"Username Error";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- }
- if($username_length>25 || $username_length<8){
- echo"Username length error";
- $_GLOBALS['$no_errors']=false;
- }
- }
- }
- function passwordValid(){
- if (empty($_POST["password"])) {
- echo"Password field is empty";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- } else {
- $password_from_form = test_input($_POST["password"]);
- $pass_length=strlen($password_from_form);
- if ($pass_length>15 || $pass_length<8) {
- echo"Password length error";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- }
- }
- }
- function semesterValid(){
- if (empty($_POST["semester"])) {
- echo"Semester field is empty";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- } else {
- $semester_from_form = test_input($_POST["semester"]);
- $semester_length=strlen($semester_from_form);
- if (!preg_match("/^[0-9]+$/",$semester_from_form)) {
- echo"Semester Error";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- }
- if ($semester_length>2) {
- echo"Semester length error";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- }
- }
- }
- function amValid(){
- if (empty($_POST["am"])) {
- echo"ΑΜ field is empty";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- } else {
- $am_from_form = test_input($_POST["am"]);
- $am_length=strlen($am_from_form);
- if ($am_length>10) {
- echo"AM length error";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- }
- }
- }
- function telephoneValid(){
- if (empty($_POST["telephone"])) {
- echo"Telephone field is empty";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- } else {
- $telephone_from_form = test_input($_POST["telephone"]);
- $telephone_length=strlen($telephone_from_form);
- if ($telephone_length<10 || $telephone_length>15) {
- echo"Telephone length error";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- }
- }
- }
- function rpasswordValid(){
- if (empty($_POST["rpassword"])) {
- echo"Confirm password field is empty";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- } else {
- $rpassword_from_form = test_input($_POST["rpassword"]);
- $pass=test_input($_POST["password"]);
- if (strcmp($rpassword_from_form,$pass) !=0) {
- echo"Confirm password does not match the password";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- }
- }
- }
- function emailValid(){
- if (empty($_POST["email"])) {
- echo"Email field is empty";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- } else {
- $email_from_form = test_input($_POST["email"]);
- if (!filter_var($email_from_form, FILTER_VALIDATE_EMAIL)) {
- echo"Invalid mail";echo "<br>";
- $_GLOBALS['$no_errors']=false;
- }
- }
- }
- function insertStudent(){
- $name_from_form = test_input($_POST["name"]);
- $lastname_from_form = test_input($_POST["lastname"]);
- $username_from_form = test_input($_POST["username"]);
- $password_from_form = test_input($_POST["password"]);
- $semester_from_form = test_input($_POST["semester"]);
- $am_from_form = test_input($_POST["am"]);
- $telephone_from_form = test_input($_POST["telephone"]);
- $email_from_form = test_input($_POST["email"]);
- $shauser = sha1($username_from_form);
- $shapass = sha1($password_from_form);
- $con = mysqli_connect('localhost','root');
- mysqli_select_db($con,"education");
- $insert_query="INSERT INTO STUDENT (username,password,am,name,surname,semester,telephone,email)VALUES
- ('$shauser','$shapass','$am_from_form','$name_from_form','$lastname_from_form','$semester_from_form','$telephone_from_form','$email_from_form')";
- if (mysqli_query($con, $insert_query)) {
- echo "New record created successfully";
- } else {
- echo "Error: " . $insert_query . "<br>" . mysqli_error($con);
- }
- mysqli_close($con);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement