使用数据库验证电子邮件

时间:2010-10-22 21:12:55

标签: php database authentication

我已经创建了一个表单,并使用PHP验证了所有内容,但无法弄清楚如何验证来自数据库的电子邮件。如果我在数据库中输入了用户名,我希望它显示错误。我有connect.php和

仅举例 -

这是我如何验证密码 -

if(!empty($_POST['password']))
{
 if($_POST['password'] != $_POST['cpass']) 
 {
  $errors[] = 'The password and confirm password do not match.';
 }
 else
 {
  $p=trim($_POST['password']);
 }
}

这是我正在尝试做的事情 -

$getusername = "SELECT username FROM users WHERE ($u,$username)";
if($getusername)
{
    echo 'Username is already in use.';
}
else
{
    $g=trim($_POST['username']);

}

这导致PARER错误。

1 个答案:

答案 0 :(得分:1)

// first define the username from the $_POST variable
// make sure to escape the value to prevent SQL injection
$username = mysql_real_escape_string(trim($_POST['username']));

// select a user with the posted username
$sql = "SELECT username FROM users WHERE username = '$username' LIMIT 1";

// run the query
$res = mysql_query($sql) or die(mysql_error());

// see if there's a result
if (mysql_num_rows($res) > 0) {
  echo 'This username is already taken';
} else {
  // .. do stuff
}