好的,有这个代码,它是一个注册表单,但我无法检查是否已经使用了用户名?它所做的只是再次在数据库中创建相同的用户名,而不是返回“用户名已被使用”,如果有人可以提供帮助,将不胜感激。
$checkusernametaken = mysql_query("SELECT FROM users (username) WHERE username=username");
if(mysql_num_rows($checkusernametaken)>=1)
{
echo"Username already exists";
}
else
{
$sql = "INSERT INTO users(username, password) VALUES(:username, :password)";
$stmt = $con->prepare( $sql );
$stmt->bindValue( "username", $this->username, PDO::PARAM_STR );
$stmt->bindValue( "password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );
$stmt->execute();
return "Registration Successful Welcome " + $checkusernametaken + "<br/> <a href='index.php'>Login Now</a>";
}
这是我使用的代码,在评论中每个人的帮助下,我得到了它,谢谢大家!
try {
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "INSERT INTO users(username, password) VALUES(:username, :password)";
$stmt = $con->prepare( $sql );
$stmt->bindValue( "username", $this->username, PDO::PARAM_STR );
$stmt->bindValue( "password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );
$stmt->execute();
return "Registration Successful <br/> <a href='index.php'>Login Now</a>";
}
catch( PDOException $e )
{
return "Username Taken <br/> <a href='register.php'>Try Again?</a>";
}