Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

INSERT & SELECT to see if entry exists

Hi

I'm having a little trouble wrapping my head around this. I want to check the table on submit to see if the email already exists. The issue I have is I make the entry but I don't think my query on the email column is working or i'm not binding to stmt correctly.

Any help would be greatly appreciated.

<?php
$date = new DateTime();

//VARIABLES & INSERT QUERY
if (isset($_POST["submit"])) {

    // Get the form fields and remove whitespace.
    $name   = strip_tags(trim($_POST["name"]));
    $name   = str_replace(array("\r", "\n"), array(" ", " "), $name);
    $mobile = strip_tags(trim($_POST["mobile"]));
    $mobile = str_replace(array("\r", "\n"), array(" ", " "), $mobile);
    $date = strip_tags(trim($_POST["date"]));
    $date = str_replace(array("\r", "\n"), array(" ", " "), $date);
    $email  = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $userID = mysqli_insert_id($success);

    //INSERT INTO DATABASE

    if ($stmt = $success->prepare(
        "INSERT INTO dbtest (name, email, mobile, date) VALUES (?, ?, ?, ?);
           SELECT email FROM dbtest WHERE email=?"
    )) {

        $stmt->bind_param("ssss", $name, $email, $mobile, $date);

        if ($stmt->execute()) {
            $stmt->store_result();

            $email_check = "";
            $stmt->bind_result($email_check);
            $stmt->fetch();

            if ($stmt->num_rows == 1) {

                echo "That Email already exists.";
                exit;

            } else {

                header('Location: success.php');
                exit();

                $stmt->close();
                $success->close();
            }
        }
    }
}

1 Answer

Andrew Shook
Andrew Shook
31,709 Points

I would make a separate query to determine whether or not the email exists in the DB before doing the insert query.

<?php 
if (isset($_POST["submit"])) {

    // Get the form fields and remove whitespace.
    $name   = strip_tags(trim($_POST["name"]));
    $name   = str_replace(array("\r", "\n"), array(" ", " "), $name);
    $mobile = strip_tags(trim($_POST["mobile"]));
    $mobile = str_replace(array("\r", "\n"), array(" ", " "), $mobile);
    $date = strip_tags(trim($_POST["date"]));
    $date = str_replace(array("\r", "\n"), array(" ", " "), $date);
    $email  = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $userID = mysqli_insert_id($success);

    //Question: is the name of your table dbtest or is that the name of your DB?
    if($smt = $success->prepare( "SELECT email FROM dbtest WHERE email=?") && $stmt->execute() ){

         if ($stmt->num_rows  == 0) {
                //no rows in the dd means the email is not used so we insert the data into the data base.
               if ($stmt = $success->prepare("INSERT INTO dbtest (name, email, mobile, date) VALUES (?, ?, ?, ?);")) {
                    // code for inserting and binding then redirect and exit
               }else{
                   //prepare failed.
               }
        }

    }else{
            //prepare failed.
        }

    //if we make this far in the code then we know that the that the email has been take. 
    header('Location: success.php');
    exit();