所以我使用一个函数来检查一个帐户是否存在。除了在已经使用名称时打印用户名之外,它的效果很好。数据库的功能确实按预期运行。在错误消息中返回$username
只是一个奇怪的问题。
尝试用户Potion,它会显示无法使用,但会显示$username
而不是Potion,在这种情况下可以使用Wolf。
if (isset($_POST['submit'])) {
if (empty($_POST['username'])) {
$error = "Username cannot be empty.";
}
elseif (preg_match('/[\'^£$%&*()}{@#~?><>,|=+¬-]/', $_POST['username']))
{
$error = "Username must contain up to 12 characters of A-z letters 0-9 numbers and _.";
}
else
{
//set the username variable
$username = $_POST['username'];
//$username = preg_replace("/[^A-Za-z0-9_]/", "", $username);
//$username=preg_replace('/[^0-9a-zA-Z_]/',"",$input)
//check the runescape website if it exists
$likelyhood = checkUsername($username);
//if already exist, update my database
if($likelyhood === 28){
$error = "Congratulations, the username $username appears to be availible!";
addNewUserToDb($username, 1);
//if availible, update my database
}
if($likelyhood < 28){
$error = 'Sorry, the username $username is already taken.';
addNewUserToDb($username, 0);
}
}
}
答案 0 :(得分:0)
如果您希望PHP解释变量,则需要将字符串放在双引号中:
$error = "Sorry, the username $username is already taken.";
答案 1 :(得分:0)
要在字符串中插入变量,您应该使用双引号:
$error = "Sorry, the username $username is already taken.";
这就是PHP doc:
注意:与双引号和heredoc语法不同,特殊字符的变量和转义序列在单引号字符串中不会扩展。
使用单引号,您需要连接字符串:
$error = 'Sorry, the username '.$username.' is already taken.';