作为序言,我对 mysql 的了解非常有限,因此我决定尝试使用 PDO,因为我听说它使用起来更安全。话虽如此,如果您在尝试帮助我时发现任何注射责任,请告诉我!无论如何,我在如何处理 PDO 查询的输出以及一般执行时遇到问题......
我尝试做的第一件事是获取表单数据,然后搜索数据库以查看是否有其他人使用相同的用户名或相同的电子邮件:
if(isset($_POST['name']) && !empty($_POST['name']) AND isset($_POST['password']) && !empty($_POST['password']) AND $_POST['password'] == $_POST['password2'] AND isset($_POST['email']) && !empty($_POST['email']) AND isset($_POST['year']) && !empty($_POST['year']) AND isset($_POST['termsandconditions']) && !empty($_POST['termsandconditions'])){
$name = $_POST['name'];
$password = $_POST['password'];
$email = $_POST['email'];
foreach ($_POST['year'] as $year);
$termsandconditions = $_POST['termsandconditions'];
$idqry = $sql->prepare("SELECT id FROM users WHERE username = :idcheck");
$idqry->bindParam(':idcheck', $name, PDO::PARAM_STR,20);
$idqry->execute();
$emailqry = $sql->prepare("SELECT id FROM users WHERE email = :idcheck");
$emailqry->bindParam(':emailcheck', $email, PDO::PARAM_STR, 40);
$emailqry->execute();
稍后,我尝试使用 PDO 查询检查重复的用户名/电子邮件并创建不同的条件:
if(!eregi("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(edu)$", $email)){
// Return Error - Invalid Email
$msg = 'The email you have entered is invalid, please try again.<br />*NOTE: You must use a .edu email to be able to register!';
}elseif($idqry != FALSE){
$msg = "That username has already been used, please try another one.";
}elseif($emailqry != FALSE){
$msg = 'That email has already been used, <a class=statusmsg href="../recovery/">need to recover your password?</a>.';
}else{
$msg = 'Your account has been made, <br /> please verify it by clicking the activation link that has been sent to your email.';
$hash = md5(rand(0,1000));
$hashedpass = password_hash($password, PASSWORD_BCRYPT);
$sqlinsert = $sql->prepare("INSERT INTO users (username, password, email, hash, year, termsandconditions) VALUES (:username, :password, :email, :hash, :year, :termsandconditions)");
$sqlinsert->execute(array(
':username' => $name,
':password' => $hashedpass,
':email' => $email,
':hash' => $hash,
':year' => $year,
':termsandconditions' => $termsandconditions));
}
}
当我尝试对此进行测试时,我总是得到“该用户名已被使用,请尝试另一个”的 $msg 输出。(即使我输入了唯一的用户名)
你看有什么不对吗?另外,是否可以绑定参数数据库插入?谢谢!