Check if email exists in sql database and output thereafter (regarding running if inside if)

So I have a form running on a page that I’m experimenting with. Atm I just have one input and that is email. Basically I want a register and if a person tries to add the same email again it will return an error msg and otherwise it will add it.

So this is the code I’ve been posting with:

Read More
<?php
    $username="user";
    $password="pass";
    $database="test";
    $email = mysql_real_escape_string( $_POST['email'] );

    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");

    $query = "SELECT * FROM enroll WHERE email='$email' ";
    $result = mysql_query($query) or die(mysql_error());

    if (mysql_num_rows($result) ) {
        print 'user is already in table';
    }
    else {
        $query = "INSERT INTO enroll VALUES ('','$email')";
        $result = mysql_query($query) or die(mysql_error());       
        print 'user added';
    }
?>

That works fine! If I add an email that already exists it says “user is already in table” and if not it says “user added”.

Problem is that before I used to run another php script which validated the input and then upon submit it added it to the DB. Now it ALWAYS says “user is already in table” even before I’ve pressed Submit.

I would like to do something like this, but when posting it doesn’t work at all and I can’t understand what’s wrong.

<?php
    //If the form is submitted
    if(isset($_POST['submit'])) {

        // Required field names
        $required = array('email');

        // Loop over field names, make sure each one exists and is not empty
        $error = false;
        foreach($required as $field) {
            if (empty($_POST[$field])) {
            $error = true;
            }
        }

            if ($error) {
                echo 'You forgot to fill in your email';
            } else {
                $username="user";
                $password="pass";
                $database="test";
                $email = mysqli_real_escape_string( $_POST['email'] );

                mysqli_connect(localhost,$username,$password);
                mysqli_select_db($database) or die( "Unable to select database");

                $query = "SELECT * FROM enroll WHERE email='$email' ";
                $result = mysqli_query($query) or die(mysqli_error());

                if (mysqli_num_rows($result) ) {
                    print 'user is already in table';
                }
                else {
                    $query = "INSERT INTO enroll VALUES ('','$email')";
                    $result = mysqli_query($query) or die(mysqli_error());
                    print 'user added';
                }
            }
    }

?>

In my world, this checks if email is supplied, checks if it exists in db and if not adds it. If it exists it will come back with an error.

What am I doing wrong?

EDIT2: @ExpertSystem This is the code I’m going with now as per your recommendation:

<?php

        if(isset($_POST['submit'])) {

            // Required field names
            $required = array('email');

            // Loop over field names, make sure each one exists and is not empty
            $error = false;
            foreach($required as $field) {
                if (empty($_POST[$field])) {
                $error = true;
                }
            }

                if ($error) {
                    echo 'You forgot to fill in your email';
                } else {
                    $username="root";
                    $password="root";
                    $host="localhost";
                    $database="test";
                    $email = mysqli_real_escape_string( $_POST['email'] );

                    $link = mysqli_connect(localhost, $username, $password);
                    mysqli_select_db($link, $database) or die("Unable to select database");

                    $query = "SELECT * FROM enroll WHERE email='$email'";
                    $result = mysqli_query($link, $query) or die(mysqli_error($link));

                    if (mysqli_num_rows($result) ) {
                        print 'user is already in table';
                    }
                    else {
                        $query = "INSERT INTO enroll (email) VALUES ('$email')";
                        $result = mysqli_query($link, $query) or die(mysqli_error($link));

                        print 'user added';
                    }
                }
        }
?>

Related posts

Leave a Reply

3 comments

  1. Since you are using the procedural form of the mysqli_ functions, you need to pass a mysqli link identifier as the first argument in mysqli_select_db(), mysqli_real_escape_string(), mysqli_query() and mysqli_error() (see also the documentation for more details). The aforementioned mysqli link identifier is the object returned by mysqli_connect().
    The modified code should look something like this:

    ...
    $link = mysqli_connect(localhost, $username, $password);
    mysqli_select_db($link, $database) or die("Unable to select database");
    $email = mysqli_real_escape_string($link, $_POST["email"]);
    ...
    $result = mysqli_query($link, $query) or die(mysqli_error($link));
    ...
    if...
        ...
    } else {
        ...
        $result = mysqli_query($link, $query) or die(mysqli_error($link));
        ...
    ...
    
  2. My head says, “mysqli_num_rows($result);” returns an integer.
    So you can’t do if(mysqli_num_rows($result)), it will always return an integer (e.g. 0 or 1)

    So try this:

     if (1==mysqli_num_rows($result)) {
                    print 'already in table';
     }
    

    Just a thought.

  3. thanks for all your help! Hopefully I’ve at least improved my bad code with your help.

    I found out what’s causing the problem and it was this.

    <input type="submit">
    

    I changed that to

    <input type="submit" name="submit">
    

    And of course now everything works. Thanks tho! 🙂