PDO提交重复记录?

时间:2013-03-10 22:12:52

标签: php mysql database pdo

我的注册码有效,但每次都会提交一份副本。例如,如果我尝试提交

Steve Smith stevesmith@gmail.com 123456789

它将在数据库中显示两次,如此

Steve Smith stevesmith@gmail.com 123456789

Steve Smith stevesmith@gmail.com 123456789

这是我的代码

$failed         = "<p class='errormsg'>Registration failed!</p>";
$register       = "<p class='errormsg'>Thank you for registering $firstname $lastname</p>";
$adduser = $con->prepare("INSERT INTO basicuserinfo(email, password, firstname, lastname) VALUES(:email, :password, :firstname, :lastname)");
$adduser->bindValue(':email', $email);
$adduser->bindValue(':password', $hash);
$adduser->bindValue(':firstname', $firstname);
$adduser->bindValue(':lastname', $lastname);
$adduser->execute();
    if(!$adduser->execute())
    {
    echo "$failed";
    }
    else
    {
    echo "$register";
    }
}

1 个答案:

答案 0 :(得分:4)

因为您要拨打execute两次,请删除第一个exeute

$failed         = "<p class='errormsg'>Registration failed!</p>";
$register       = "<p class='errormsg'>Thank you for registering $firstname $lastname</p>";
$adduser = $con->prepare("INSERT INTO basicuserinfo(email, password, firstname, lastname) VALUES(:email, :password, :firstname, :lastname)");
$adduser->bindValue(':email', $email);
$adduser->bindValue(':password', $hash);
$adduser->bindValue(':firstname', $firstname);
$adduser->bindValue(':lastname', $lastname);
// $adduser->execute();    // <<== this should be removed,
                           //  the reason for duplicate records
    if(!$adduser->execute())
    {
       echo "$failed";
    }
    else
    {
       echo "$register";
    }