-2

Problem & Explanation

Hello I have just coded a function that first does checking if account exists in database with that name, and then if email exists in database with that entered email.

If not, return true + insert data.

But in this case, nothing happens on submit, it just shows the form, but doesn't inserts the data.. What is wrong with it?

function createAccount($name, $password, $email)
{
    global $pdo;

    $check_in = $pdo->prepare("SELECT * FROM users WHERE user_name = :username LIMIT 1");
    $check_in->execute( array(':username' => $name) );

    if (!$check_in->rowCount())
    {
        $check_in = email_exists($email);
        if ($check_in === false)
        {
            $insert_in = $pdo->prepare
            ("
                INSERT INTO 
                users 
                (user_name, user_password, user_email) 
                VALUES 
                (:name, :password, :email)
            ");
            $insert_in->execute( array 
            (
                ':name' => $name,
                ':password' => $password,
                ':email' => $email
            ));

            return true;
        }
        else
        {
            return 'exists';
        }
    }
    else
    {
        return 'user_in_use';
    }   
}

function email_exists($email)
{
    global $pdo;

    $check = $pdo->prepare("SELECT * FROM users WHERE user_email = :email LIMIT 1");
    $check->execute( array(':email' => $email) );
    if ($check->rowCount())
    {
        return true;
    }
    else
    {
        return false;
    }
}

This is how I make up the register:

# Creating shortcuts
if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email']))
{   
    $name = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];
}

# Creating errors array

$errors = array();

if (isset($_POST['submit']))
{
    $check_in = createAccount($name, $password, $email);

    if ($check_in === true)
    {
        echo 'Created account sucessfully!';
    }
    else if ($check_in == 'already_in_use')
    {
        echo 'Could not create account because name already in use..';
    } 
    else if($check_in == 'exists')
    {
        echo 'Email already in use..';
    }
}

Question:

What is wrong with this code & how do I fix this? I have no errors at all. It just won't insert any data to the Database.

Yes, the PDO connection & statements are right, because the login works perfectly. Thanks a lot!

EDIT!

    if ($check_in === true)
    {
        echo 'Created account sucessfully!';
    }
    else if ($check_in == 'already_in_use')
    {
        echo 'Could not create account because name already in use..';
    } 
    else if($check_in == 'exists')
    {
        echo 'Email already in use..';
    } else { 
    echo 'Error is there...'; 
    }

It's echoing 'Error is there...' apon submit!

Jony Kale
  • 189
  • 1
  • 5
  • 17
  • Could you show the form? Maybe there's something wrong? – bwoebi Apr 17 '13 at 19:48
  • Just added the forms. – Jony Kale Apr 17 '13 at 19:51
  • http://stackoverflow.com/questions/15990857/reference-frequently-asked-questions-about-pdo#15990858 – Your Common Sense Apr 17 '13 at 19:56
  • A classic mistake I make in this case is finding out that there multiple tables with the same name but in different schemas. Since a a schema is not specified maybe its going to a table you don't expect. – Useless Intern Apr 17 '13 at 19:56
  • @YourCommonSense I already have error reporting, whenever I have an error in the syntax, it will say. I have already had a few ones and fixed them, No errors now and still doesn't work. – Jony Kale Apr 17 '13 at 20:01
  • start debugging it then. Anyway "I have a code it doesn't work" question is not suitable for Stack Overflow. – Your Common Sense Apr 17 '13 at 20:06
  • I found the error but I can not find out what causing it.. please read my question I edited it – Jony Kale Apr 17 '13 at 20:07
  • Try to insert `echo` in your script. `echo print_r($_POST, true);` before `# Creating shortcuts` and some `echo` in each `if - else` branch to see what is real in your script. – Victor Apr 17 '13 at 20:29

1 Answers1

-1

I just want to slap myself!..... The problem was: The fields were set as INT, therefore we could not store anything but ints...

Jony Kale
  • 189
  • 1
  • 5
  • 17