user6279040
user6279040

Reputation:

How to check if email exists in my database phpmyadmin

my register.php file

I am trying to check whether or not the email being registered on the form input already exists within my database on phpmyadmin. How can I go about doing this without breaking my code.

 <?php

    session_start();

    if( isset($_SESSION['user_id']) ){
        header("Location: index.php ");
    }

    require 'database.php';

    $message = '';

    if(!empty($_POST['email']) && !empty($_POST['password'])):

        // Enter the new user in the database

        $sql = "INSERT INTO signup (name, surname,email, password) VALUES (:name, :surname,:email, :password)";
        $stmt = $conn->prepare($sql);

        $stmt->bindParam(':name', $_POST['name']);
        $stmt->bindParam(':surname', $_POST['surname']);
        $stmt->bindParam(':email', $_POST['email']);
        $stmt->bindParam(':password', password_hash($_POST['password'], PASSWORD_BCRYPT));

    if( $stmt->execute() ):
            $message = 'Successfully created new user';
        else:
            $message = 'Sorry there must have been an issue creating your account';
        endif;

    endif;

    ?>

Upvotes: 0

Views: 909

Answers (1)

miku2s
miku2s

Reputation: 23

Here is a function using PDO who return 1 if an email already exist :

 function check_mail_exist($con, $mail){
    $sql = $con->prepare("SELECT * FROM table_name WHERE column_mail='".$mail."'; ");
    $sql->execute();
    $result = $sql->rowCount();
    if ($result > 0)
        return 1;
    return 0;
}

Upvotes: 1

Related Questions