Selecting username and email from database if already there using php prepared statement

I’m creating a registration form.. in which I want to check if any username and email id is present in the database then I want to display a message that email or username is already registered using php prepared statement. But I’m not able to do this…please look at my codes where I’m wrong….???

<?php  
include 'config.php';

$name=check_input($_POST['name']);
$username=check_input($_POST['username']);
$email=check_input($_POST['email']);
$phone=check_input($_POST['phone']);
$pass=check_input($_POST['pass']);
$cpass=check_input($_POST['cpass']);
$pass=md5($pass);
$cpass=md5($cpass);
$created=date('Y-m-d');

$name=$conn->real_escape_string($name);
$username=$conn->real_escape_string($username);
$email=$conn->real_escape_string($email);
$phone=$conn->real_escape_string($phone);
$pass=$conn->real_escape_string($pass);

if($pass==$cpass){

    $sql=$conn->prepare("SELECT username,email FROM users WHERE username=? OR email=?");
    $sql->bind_param("ss",$username,$email);
$sql->execute();
$sql->store_result();
if($row=$sql->num_rows==1){
    if($username==$row['username']){
    echo "Username not available try different!";
    }
    if($email==$row['email']){
        echo "Email already exists!";
    }
}
else{
    $stmt = $conn->prepare("INSERT INTO users (name, username, email, phone, pass, created) VALUES (?, ?, ?, ?, ?, ?)");
        $stmt->bind_param("sssiss", $name,$username,$email,$phone,$pass,$created);
        if($stmt->execute()){
            echo"Registered Successfully. Login Now!";
        }
        else{
            echo "Something went wrong!";
        }
}
}
else{
    echo "Password didn't matched!";
}
function check_input($data){
    $data=trim($data);
    $data=stripslashes($data);
    $data=htmlspecialchars($data);
    return $data;
}

?>

For more info please click herehttps://stackoverflow.com/q/50842375

Have a question or need a custom quote?
info@smartinfocare.com