检查用户名是否存在POSTBACK

时间:2014-06-01 07:14:22

标签: php mysql xhtml

我想使用POSTBACK方法检查用户是否存在于mysql表中。我正在学习mysql,我知道它很快就会被删除,但我现在无法改变。我希望警报在用户名文本框旁边弹出,如果它已经存在的话。此时它不起作用。我有一个类似的密码和密码确认代码,但我认为这有所不同,因为我需要一个查询。这就是我所拥有的:

<?php
$passErr = $pass1Err = "";
$passw = $passw1 = "";
$userErr="";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["password"])) {
     $passErr = "Password is required";
                                  }
if (empty($_POST["passconfirm"])) {
     $pass1Err = "Password confirmation is required";
                                 }
if ($_POST['password']!= $_POST['passconfirm'])
 {
     $passErr = "Passwords must be the same";
     $pass1Err = "Passwords must be the same";

 }

}
else {

if (isset($_REQUEST["submit"]))
{

   if (isset($_POST["submit"]))
   {

     $firstname = mysql_real_escape_string($_POST["gname"]);
     $middlename = mysql_real_escape_string($_POST["mname"]);
     $lastname = mysql_real_escape_string($_POST["surname"]);
     $user = mysql_real_escape_string($_POST["username"]);
     $addy = mysql_real_escape_string($_POST["address"]);
     $post = mysql_real_escape_string($_POST["postcode"]);
     $sta = mysql_real_escape_string($_POST["state"]);
     $telephone = mysql_real_escape_string($_POST["tel"]);
     $pass = mysql_real_escape_string($_POST["password"]);
     $systemuser= mysql_real_escape_string($_POST["susername"]);

$sql2 = "SELECT username FROM users WHERE username= '$user'"; 
 $rs = mysql_query($sql2, $conn) 
 or die ('Problem with query' . mysql_error());

$num_rows = mysql_num_rows($rs);

       if(isset($_POST['username'])) {
                     if($num_rows != 0){
                $userErr = "Username already exists";
                                }
                                       }


}
}
mysql_close($conn);
} 
?>

这就是我在表格中得到的结果:

<label>Chosen Username:</label> <input type="text" name="username" value="<?php 
echo $userErr;?>"/><span class="error">* <?php echo $userErr;?></span><br />

<label>Password:</label> <input type="password" name="password" value="<?php 
echo $passw;?>"/><span class="error">* <?php echo $passErr;?></span><br />
<br />
<label>Password confirmation:</label> <input type="password" name="passconfirm" value="<?php 
echo $passw1;?>"/><span class="error">* <?php echo $pass1Err;?></span><br />

2 个答案:

答案 0 :(得分:0)

要实现您的目标,而不是使用

   $num_rows = mysql_num_rows($rs);
   if(isset($_POST['username'])) {
       if($num_rows != 0){
           $userErr = "Username already exists";
        }
    }

替换为

   $num_rows = mysql_num_rows($rs);
   if(isset($_POST['username'])) {
       $userErr = "Username field cannot be empty";
   }elseif($num_rows > 0){
           $userErr = "Username already exists";
   }

这样,如果Username字段为空,则会出错。这也将实现检查数据库以查看用户是否存在的目标。

答案 1 :(得分:0)

你需要知道带有用户名的请求是否返回了一些错误信息,否则会插入新用户,

 $sql2 = "SELECT * FROM users WHERE username= '$user'"; 
 $rs = mysql_query($sql2, $conn) or die ('Problem with query' . mysql_error());

   if($num_rows=mysql_fetch_array($rs)){ 

         $userErr = "Username already exists";

         // ....
     } else{
        // $SQL = "INSERT INTO users SET ... 
     }