gpt4 book ai didi

php - if-else 语句 strlen 不工作 php

转载 作者:行者123 更新时间:2023-11-29 00:00:18 25 4
gpt4 key购买 nike

我正在尝试创建有效的注册验证,但我遇到了问题。验证工作但存在错误,即使用户输入正确的信息也不会执行 INSERT 语句。但是,当删除“elseif”语句时,界面上会回显错误,但即使用户表单中存在错误,数据仍会被插入;例如只有字母的密码。如果有人可以修复此错误,我将不胜感激。完整的验证如下。谢谢瓦西姆

<?php include "connection2.php" ?>
<?php
session_start();
if (empty($errors)) {
if(isset($_POST['submitted'])) {

$firstname=$_POST['Firstname'];
$lastname=$_POST['Lastname'];
$username=$_POST['Username'];
$password= $_POST['Password'];
$email=$_POST['Email'];


if ($username&&$password&&$email) {

if (strlen($username)>10) {
echo "Username is too long (Max 10 Characters)";
}
else {

//check password length
if (strlen($password)>15||strlen($password)<5) {
echo "Password must be 5 to 15 characters<br>";
}
if (!preg_match("#[0-9]+#", $password)) {
echo "Password must include at least one number!<br>";
}
if (!preg_match("#[a-zA-Z]+#", $password)) {
echo "Password must include at least one letter!<br>";
}

elseif ((!strlen($username) >10) and

(!strlen($password)>15||!strlen($password)<5) and
(preg_match("#[0-9]+#", $password)) and
(preg_match("#[a-zA-Z]+#", $password))) {


$sql = mysql_query
("INSERT INTO users(firstname, lastname,username, password, email)

VALUES ('$firstname','$lastname','$username',

'$password','$email')");

md5($password);
//register the user!

echo "Your Are Now a Member";




}

}
}

mysql_close();
}}
else { ?>
<?php }
?>

最佳答案

您的 elseif 仅附加到最后一个 if。我不确定你是否把所有的倒置逻辑都弄对了。但一个更简单的解决方案是只设置一个变量来指示是否有任何验证检查失败,然后对其进行测试。

if ($username && $password && $email) {

if (strlen($username)>10) {
echo "Username is too long (Max 10 Characters)";
}
else {
$error = false;
//check password length
if (strlen($password)>15||strlen($password)<5) {
echo "Password must be 5 to 15 characters<br>";
$error = true;
}
if (!preg_match("#[0-9]+#", $password)) {
echo "Password must include at least one number!<br>";
$error = true;
}
if (!preg_match("#[a-zA-Z]+#", $password)) {
echo "Password must include at least one letter!<br>";
$error = true;
}

if (!$error) {
// insert new users
}
}
}

关于php - if-else 语句 strlen 不工作 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30133914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com