【发布时间】:2018-04-01 12:33:59
【问题描述】:
所以我有这些输入来自我的表单firstname、lastname、username、email、password。
但是当所有这些都包含用户提供的任何信息时,我只想填写注册表并将其保存到我的数据库中。我现在有这个,但如果密码和/或密码确认字段不匹配,它只会显示错误。
if( !empty ($_POST)){
if($_POST['password'] == $_POST['password_confirmation']) {
$user = new User();
$user->setFirstName($_POST['firstname']);
$user->setLastName($_POST['lastname']);
$user->setUsername($_POST['username']);
$user->setEmail($_POST['email']);
$user->setPassword($_POST['password']);
if($user->register()){
$user->login();
}
else{
$error_confirmation = true;
// This gives error in the front end if both of the PW do no match each other
}
}
我想我应该做这样的事情?
if( !empty ($_POST['firstname']) && !empty ($_POST['lastname']) && !empty ($_POST['username']) && !empty ($_POST['email']) && !empty ($_POST['password'])){
编辑:
好吧,我想我只是自己想通了。该页面不发送信息,但也不显示$error_firstname=true; 消息?
if( !empty ($_POST['firstname']) && !empty ($_POST['lastname']) && !empty ($_POST['username']) && !empty ($_POST['email']) && !empty ($_POST['password'])){
if( empty ($_POST['firstname'])){
$error_firstname = true;
}
if( empty ($_POST['lastname'])){
$error_lastname = true;
}
if( empty ($_POST['username'])){
$error_username = true;
}
if( empty ($_POST['email'])){
$error_email = true;
}
else if($_POST['password'] == $_POST['password_confirmation']) {
$user = new User();
$user->setFirstName($_POST['firstname']);
$user->setLastName($_POST['lastname']);
$user->setUsername($_POST['username']);
$user->setEmail($_POST['email']);
$user->setPassword($_POST['password']);
if($user->register()){
$user->login();
}
}
else{
$error_confirmation = true;
// show pw confirmation error
}
}
最终编辑:这确实解决了问题。
我对此进行了修复,并在输入 html 标记上使用了required。
第一行检查是否给出了所有输入,然后继续到下一个if,它检查 pw 是否与 pw 确认匹配,然后保存到 DB 中
if( !empty ($_POST['firstname']) && !empty ($_POST['lastname']) && !empty ($_POST['username']) && !empty ($_POST['email']) && !empty ($_POST['password'])){
if($_POST['password'] == $_POST['password_confirmation']) {
$user = new User();
$user->setFirstName($_POST['firstname']);
$user->setLastName($_POST['lastname']);
$user->setUsername($_POST['username']);
$user->setEmail($_POST['email']);
$user->setPassword($_POST['password']);
if($user->register()){
$user->login();
}
}
else{
$error_confirmation = true;
// if no -> $error tonen
}
}
【问题讨论】:
-
如果有一个空值作为名字或电子邮件的用户对您来说不是有效用户,那么是的 - 验证您的输入。在你的领域模型中强制执行它们。
-
empty ($_POST['firstname'])永远不会是真的,你已经检查过它(和其他的)不是空的。在比较之前,您确实需要检查$_POST['password_confirmation']是否为空。如果您关心隔离哪个字段为空,请不要打扰多重检查条件;一次检查每个元素。 -
我修好了,检查编辑
-
确认信息可能为空;导致通知。
-
@mickmackusa 哦,我忘了在帖子里告诉它,但 PW 需要 8 个字符长,所以它不能为空。 if(strlen($password)
标签: php validation conditional registration is-empty