Access Control using PHP & MySQL

I started coding with PHP last 2 months after which I encountered a very challenging problem. I have been traying to use PHP & MySQL to create Login and Registration pages. My Login page contains two textboxes: one for Email and another for Password.
My Registration page contains three textboxes: Email, Password. RetypePassword.
In the Registration page, before you can register, the script must have a way of checking;

  • if textpassword is equal to textretypepassword
  • if the email has not been previously created by another user.
    My Codes are bellow on controller page
<?php 
// Creating registration form
if (isset($_GET['register']) )
// If clicked on "registration", display regisration page.
{
include 'registration.php';
exit();
}
if (isset( $_POST['action']) and $_POST['action'] == 'create' ) 
// After filling the registration form and click on submit button,
// execute the following.
{
$link = mysqli_connect('myhost','myusername', 'mypassword');
if (!$link)
{
$output = 'Unable to connect to database server: ' . mysqli_error($link);
include 'welcome.php';
     exit();
}


if (!mysqli_select_db($link, 'my_dbase_name'))    
{
$output = 'Unable to locate ngspc database establishement .';
include 'welcome.php';
exit(); 
}
// Assign variables to textpassword and textconpassword

$textemail = $_POST['textemail1'];
$password = $_POST['textpassword'];
$confirm = $_POST['textconpasswoed'];
// Check if the two texboxes has the same character.
if($password == $confirm)
{ 
$ema = mysqli_real_escape_string($link, $_POST['textemail1']);
//Search for Existing email
$sql = "SELECT COUNT(*) FROM tb_login WHERE fd_email = '$ema'";
$result = mysqli_query($link, $sql);
if (!mysqli_query($link, $sql))
{
$error = 'Error counting your record' . mysqli_error($link);
    include 'help.php';
    exit();
}
$row = mysqli_fetch_array($result)
{
if ($row[0] > 0)
// If found matching email, display account_exist.php page.
     {
     include 'account_exist.php';
     $textemail = $_POST['textemail1'];
     exit();
     }
     else
     {
// If no match, submit the email, password and retypepassword     
$pass = mysqli_real_escape_string($link, $_POST['textpassword']); 
$conpass = mysqli_real_escape_string($link, $_POST['textconpasswoed']);
$sql = "INSERT INTO tb_login SET
fd_email= ' $ema' ,
fd_pword= ' $pass ' ,
fd_name= ' $conpass '";
     }
}
}    
else
{
// include registration_match.php to indicate that the textpassword and textretypepassword did not match
include 'registration_match.php';
$textemail = $_POST['textemail1'];
exit();
} 

}
// the visible page that contains the link "register".
include 'log_in.php'; 
?>

These only work for the first click on submit button.
I will be glad if you guys can help me fix my problem, my sincerely regards.