doujing6436 2013-04-17 19:44
浏览 14

我的注册功能有什么问题?

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!

  • 写回答

1条回答 默认 最新

  • douwei1174 2013-04-17 20:32
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line