【发布时间】:2014-03-21 16:40:05
【问题描述】:
if(isset($_POST['submitRegister'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$email = $_POST['email'];
if(!preg_match('#^[a-zA-Z0-9_-]+$#', $username))
$error1 = 'Username can only contain: A-Z a-z 0-9 _ - ';
if(!isset($username) || empty($username))
$error1 = 'Please enter your Username';
if(!preg_match("#[0-9]+#", $password))
$error2 = 'Password must include at least one number';
if(!isset($password) || empty($password))
$error2 = 'Please enter your Password';
if($password != $password2)
$error3 = 'Passwords do not match';
if(!isset($password2) || empty($password2))
$error3 = 'Please confirm your Password';
if(!preg_match("#^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}+$#", $email))
$error4 = 'That e-mail does not appear to be valid';
if(!isset($email) || empty($email))
$error4 = 'Please enter your E-mail address';
if(!isset($_POST["terms"]))
$error5 = 'You must accept the Terms and Conditions';
else {
require_once 'server.php';
require_once 'dbconn.php';
}
}
我在让它正常工作时遇到了一些麻烦。 如您所见,我有多个 if 条件。事实上还有更多,但我已经编辑了它以使帖子更短一些。我经历了很多 if else tuts 并认为我理解它,但显然不是。
我的问题是,只要我不勾选术语复选框(最后一个 if 语句),一切正常。但是如果我勾选复选框,它将尝试连接到数据库,无论之前的字段是空的还是没有正确填写。
考虑到我上一篇文章中我的 if 顺序错误(从后到前),我将 if 语句放在最前面,将用户名放在最后。我认为这解决了它,但只要我输入用户名它就会连接,无论其他字段是否为空。所以这在这种情况下不起作用。 希望有人能帮忙,非常感谢。
【问题讨论】:
-
一目了然,我怀疑你想要
elseif在那里......
标签: php forms if-statement