我正在使用IBM Db2和PHP创建一个简单的注册页面,所有数据都保存在Db2数据库中。但是,我不能保留已经存在的用户名的约束。即使用户名已经存在于Db2中,它也在注册用户。另外,它使用任何输入的密码登录!
我无法理解何时在数据库中显示条目,这意味着数据已找到与数据库的正确连接。那么为什么它没有抓住约束逻辑。
我刚刚在Db2中更改了PHP的MySQL函数,并且有些函数给了我db2_exec()
和db2_fetch_assoc
这样的错误提示。
答案 0 :(得分:0)
$user_check_query= "SELECT * FROM users WHERE username ='$username'"
$result = db2_exec($db,$user_check_query);
$user = db2_fetch_assoc($result);
if(!empty($user)){ // Not empty mean database already exist this username
array_push($errors,"Username exists");
}
答案 1 :(得分:0)
上面提出的查询在以下代码段中得以解决;
if(isset($_POST['signup'])){
$username = ($_POST['username']);
$email = ($_POST['email']);
$password = ($_POST['password']);
$confirm_password = ($_POST['confirm_password']);
//form validation
if(empty($username)) {array_push($errors, "Username is required");}
if(empty($email)) {array_push($errors, "Email is required");}
if(empty($password)) {array_push($errors, "Password is required");}
if($password != $confirm_password) {array_push($errors, "Passwords do not match");}
if(strlen($password)<6){array_push($errors, "Password must be at least 6 characters long");}
if (!preg_match($password_requirements, $password) ) {array_push($errors,"Password must contain at least one upper case , one lower case and one digit" );}
//check db for existing user with same username
$check_username = "SELECT * FROM people WHERE username = '$username'";
$check_email = "SELECT * FROM people WHERE email = '$email'";
$res_username = db2_exec($db, $check_username);
$user_username = db2_fetch_assoc($res_username);
$res_email = db2_exec($db, $check_email);
$user_email = db2_fetch_assoc($res_email);
if(!empty($user_username)){
array_push($errors, "Username already exists!");
}
if(!empty($user_email)){
array_push($errors, "Email already exists!");
}
//register user if no error
elseif (count($errors) == 0) {
//$password = md5($password);
$query = "INSERT INTO people (username, email, password)
VALUES ('$username', '$email', '$password')";
db2_exec($db,$query) or die("couldn't execute query..".db2_stmt_errormsg());
$_SESSION['username']= $username;
$_SESSION['success']= "You are now logged in";
//echo "you are now logged in";
header('Refresh: 0; URL=index.php', true, 301);
}
}
答案 2 :(得分:0)
只需添加另一个查询以检查用户名和密码
$check_username_password= "SELECT * FROM users WHERE username ='$username' AND
password = '$password'";
$result = db2_exec($db,$check_username_password);
$user = db2_fetch_assoc($result);
if(!empty($user)){ // username match with the password
// Set your session here
// Redirect to the page you want
}
else{
// Show your error here
}