[英]The !preg_match() function in a condition statement is false always
请参见以下代码。 我认为是正确的。 当我将另一个字符与字母和数字一起使用或单独使用时,我应该only the letters and numbers
看到only the letters and numbers
消息。 但是,即使我只使用字母和数字,也再次看到了only the letters and numbers
。
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
$username = $_POST['username'];
$login = $_POST['pass'];
$repass = $_POST['repass'];
if(empty($username) || empty($login) || empty($repass)) {
echo "one or more fields is empty";
}
else if(!preg_match("[A-Za-z0-9]",$username) || !preg_match("[A-Za-z0-9]", $login) || !preg_match("[A-Za-z0-9]",$repass)){
echo "only the letters and numbers";
}
else if($login != $repass){
echo "the password is not match with the repassword";
}
else {
$connection = mysqli_connect("127.0.0.1", "root", "", "hello");
if(!$connection) { echo "error in connecting to the database";}
else {
$query = mysqli_query($connection,"SELECT * FROM test WHERE username='$username'");
$rows = mysqli_num_rows($query);
if($rows == 1 ) {
echo "there is this username currently";
}
else {
$sql = "INSERT INTO test (username, login, repass) VALUES ('$username', '$login', '$repass')";
if(mysqli_query($connection,$sql)) {
echo "you are register";
} else{
echo "please again";
mysqli_close($connection);}
} } } }
else {
echo "invalid request";
}
?>
只需添加一个定界符
else if(preg_match("#[^a-z0-9]+#i",$username))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.