1

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

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";
    }
}
4

1 回答 1

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";
    }
于 2013-03-10T22:14:11.030 回答