Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • A simple registration and login form and connectivity through database in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 1.54k
    Comment on it

    For Javascript validation please refer this link:  http://findnerd.com/account/#url=/list/view/Simple-Javascript-form-validation/15856/
     

     

    FOR REGISTERING: Here is the code for registering the user and saving data in data.

    <?php
         ob_start(); 
      		include "db.php";
      		extract($_POST);
      		if($_SERVER["REQUEST_METHOD"] == "POST")
      		{
              $sql = "SELECT * FROM emp where email = $email";
              $result = $conn->query($sql);
              if ($result->num_rows > 0) {
                  echo "<script type='text/javascript'> alert('Username already registered')</script>";
              }
              else{
                $sql = "INSERT INTO emp (name,email,password) VALUES ('$name','$email','$password')";//use md5() for password to encrypt it
                if ($conn->query($sql) == TRUE) {
                  header('Location:form.php');
                  // echo "<script>location.href='form.php'</script>";
                }
                else{
                  echo "<script type='text/javascript'> alert('Error: ". $sql."::". $conn->error."')</script>";   
                }  
              }
      		}
          $conn->close();
      	?>

    In the above the file that I have included contains the connection to the database. First we have to check whether the username already exists in the database or not. For that I am getting data from the database by SQL SELECT query where the query matches the given username.

    If the query does not return any value them i am inserting data into database.

     

    FOR LOGGING IN: Here is the code for logging in-

    	<?php
                    ob_start();   
      		include "db.php";
      		extract($_POST);
      		if($_SERVER["REQUEST_METHOD"] == "POST")
      		{
    			$sql = "SELECT * FROM emp where email = '$email' and password = '$password'";
    			$result = $conn->query($sql);
    			if ($result->num_rows > 0) {
    				 // echo "<script>location.href='home.php'</script>";
    				 header('Location:home.php');
    			}
    			else{
    				echo "<script type='text/javascript'> alert('Incorrect s /PASSWORD')</script>";
    			}
      		}
    		$conn->close();
      	?>

    Here i simply running a SQL query to select data from the database if the query matches the username and password.

    A simple registration and login form and connectivity through database in php

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: