我有一个脚本来注册用户或检查用户信息是否有任何错误,如非匹配密码或用户是否已存在等。
当我打印错误消息时,如果已经使用了用户名,$ errorMessage的值始终为0,我不确定原因:
<?php
//get user name and passwords
$errorMessage = "Error: ";
$successMessage;
if ($p1 == ""){
$errorMessage = $errorMessage + "You left your password blank. ";
echo $errorMessage;
$fail = $fail + 1;
}
if ($p2 == ""){
$errorMessage = $errorMessage + "You did not type your password in a second time. ";
$fail = $fail + 1;
echo $errorMessage;
}
//hash passwords
$hash1 = sha1($p1);
$hash2 = sha1($p2);
$fail = 0;
//check if user name is taken
if($hash1 != $hash2){
$fail = $fail + 1;
$errorMessage = $errorMessage + "Your passwords do not match. ";
echo $errorMessage;
}
if ($numRows > 0){
$errorMessage = $errorMessage + "That User name is already taken, please try another username. ";
$fail = $fail + 1;
echo $errorMessage;
}
if ($userName == ""){
$errorMessage = $errorMessage + "You left your username blank. ";
$fail = $fail + 1;
echo $errorMessage;
}
if ($email == ""){
$errorMessage = $errorMessage + "You left your email blank. ";
$fail = $fail + 1;
echo $errorMessage;
}
//do not register user if fails
if( $fail > 0){
//print fail message JSON
$successMessage = "fail";
}
//register user
else{
//add user to database
//print json
$d = array('status' => "$successMessage" , 'error' => "$errorMessage" );
$jsonCode = json_encode($d);
header('Content-Type: application/json');
echo $jsonCode;
?>
答案 0 :(得分:7)
PHP的concat运算符是句点或点(.
)而不是加号(+
)。
答案 1 :(得分:0)
你必须像这样连接字符串:
$errorMessage = $errorMessage . "You left your password blank. ";
答案 2 :(得分:0)
您没有正确连接。在PHP中,您使用..来连接。我为你修好了代码:
<?php
//get user name and passwords
$errorMessage = "Error: ";
$successMessage;
if ($p1 == ""){
$errorMessage = $errorMessage."You left your password blank. ";
echo $errorMessage;
$fail = $fail + 1;
}
if ($p2 == ""){
$errorMessage = $errorMessage."You did not type your password in a second time. ";
$fail = $fail + 1;
echo $errorMessage;
}
//hash passwords
$hash1 = sha1($p1);
$hash2 = sha1($p2);
$fail = 0;
//check if user name is taken
if($hash1 != $hash2){
$fail = $fail + 1;
$errorMessage = $errorMessage."Your passwords do not match. ";
echo $errorMessage;
}
if ($numRows > 0){
$errorMessage = $errorMessage."That User name is already taken, please try another username. ";
$fail = $fail + 1;
echo $errorMessage;
}
if ($userName == ""){
$errorMessage = $errorMessage."You left your username blank. ";
$fail = $fail + 1;
echo $errorMessage;
}
if ($email == ""){
$errorMessage = $errorMessage."You left your email blank. ";
$fail = $fail + 1;
echo $errorMessage;
}
//do not register user if fails
if( $fail > 0){
//print fail message JSON
$successMessage = "fail";
}
//register user
else{
//add user to database
//print json
$d = array('status' => "$successMessage" , 'error' => "$errorMessage" );
$jsonCode = json_encode($d);
header('Content-Type: application/json');
echo $jsonCode;
?>