Php – How to validate email id and password & confirm password in php

emailphp

i doing register form in php. i dont know how to validate my entered email and (password and confirm password) in php. For email if i enter same id "It should tell my email alread registered" and for password and confirmpassword should check whether it same or not. I have tried email in my coding. it throwing error email already registed but i want to display error in login screen. Please help for this two things. i am very new to php. sorry for my poor english.
I have return my code below,

<?php
require('config.php');
// If the values are posted, insert them into the database.
if (isset($_POST['username']) && isset($_POST['password'])){
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $cpassword = $_POST['confirmpassword'];
    $slquery = "SELECT 1 FROM register WHERE email = '$email'";
    $selectresult = mysql_query($slquery);
    if(mysql_num_rows($selectresult)>0)
    {
        die('email already exists');
    }

    $query = "INSERT INTO `register` (username, password,confirmpassword, email) VALUES ('$username', '$password', '$cpassword', '$email')";
    $result = mysql_query($query);
    if($result){
        $msg = "User Created Successfully.";
    }
   }
?>
<!DOCTYPE html>
<html>
<head>
    <title>Login screen</title>
    <style type="text/css">
    .register-form{
        width: 500px;
        margin: 0 auto;
        text-align: center;
        padding: 10px;
        padding: 10px;
        background : #c4c4c4;
        border-radius: 10px;
        -webkit-border-radius:10px;
        -moz-border-radius:10px;
    }
    .register-form form input{padding: 5px;}
    .register-form .btn{
        background: #726E6E;
        padding: 7px;
        border-radius: 5px;
        text-decoration: none;
        width: 50px;
        display: inline-block;
        color: #FFF;
    }
    .register-form .register{
        border: 0;
        width: 60px;
        padding: 8px;
    }
    </style>
</head>
<body>
    <div class="register-form">
    <?php
        if(isset($msg) & !empty($msg)){
        echo $msg;
    }
    ?>
    <?php
    if(isset($errormes))
    {
        echo $errormes;
    }
    ?>
        <h1>Register</h1>
        <form action="" method="POST">
            <p><label>User Name: </label>
            <input type="text" id="username" name="username" placeholder="Username">
            </p>
            <p><label>E-Mail&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :</label>
            <input id="email" name="email" type="email">
            </p>
            <p><label>Password&nbsp;&nbsp; :</label>
            <input id="password" name="password" type="password">
            </p>
            <p><label>confirm Password &nbsp;&nbsp; :</label>
            <input id="confirmpassword" name="confirmpassword" type="password">
            </p>
            <input type="submit" name="submit" value="register" class="btn register">
            <a class="btn" href="login.php">Login</a>
        </form>
    </div>
</body>
</html>

Best Solution

The die(), function will effectivly stop the execution of your script. This meaning no more code will run after that line.

You probably would like to use something as a header() to redirect the user to a page dispalying the incorrect loginscreen.

Or you could include the page, like you do in your example. Something like this:

<?php
require('config.php');
// If the values are posted, insert them into the database.
if (isset($_POST['username']) && isset($_POST['password'])){
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $cpassword = $_POST['confirmpassword'];
    $slquery = "SELECT 1 FROM register WHERE email = '$email'";
    $selectresult = mysql_query($slquery);
    if(mysql_num_rows($selectresult)>0)
    {
         $msg = 'email already exists';
    }
    elseif($password != $cpassword){
         $msg = "passwords doesn't match";
    }
    else{
          $query = "INSERT INTO `register` (username, password,confirmpassword, email) VALUES ('$username', '$password', '$cpassword', '$email')";
          $result = mysql_query($query);
          if($result){
             $msg = "User Created Successfully.";
          }
    }
   }
?>

replace your first code block with this.