duanfei8399 2015-05-09 21:33
浏览 48
已采纳

用户名或电子邮件已存在

Is it possible to split the Username or Email already exists into specific error messages?

I'm working on this user registration script, if you have any tips about the rest of code given would be much appreciated

if(isset($_POST['register'])){ 
  if( 
     // check if posts not empty
     empty($_POST['username']) || 
     empty($_POST['email']) || 
     empty($_POST['password']) || 
     empty($_POST['re_password']) || 
     $_POST['password'] != $_POST['re_password'] 
   ){ 
     // if a field is empty, or the passwords don't match make a message 
     error = '<p>'; 
       if(empty($_POST['username'])){ 
         $error .= 'No username given<br>'; 
        } 
        if(empty($_POST['email'])){ 
            $error .= 'No email given<br>'; 
        } 
        if(empty($_POST['password'])){ 
            $error .= 'No password given<br>'; 
        } 
        if(empty($_POST['re_password'])){ 
            $error .= 'You must re-type your password<br>'; 
        } 
        if($_POST['password'] != $_POST['re_password']){ 
            $error .= 'Passwords don\'t match<br>'; 
        } 
        $error .= '</p>'; 
    }
    else{ 
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $password = $_POST['re_password'];
        $query = mysqli_query(
            $conn,"SELECT * FROM members WHERE username = '". $username ."' OR email = '". $email ."'");
        if(mysqli_num_rows($query) > 0){
            echo "Username or Email already exist";
        }       
        else {
            $sql = "INSERT INTO members (username, password, email)
                    VALUES
                      ('$_POST[username]',
                       '$_POST[email]',
                       '$_POST[password]')";
            if (mysqli_query($conn, $sql)) {
                echo "Registered";
            } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($conn);
            }
        }
    } 
} 
if(isset($error)){ 
    echo $error; 
    unset($error); 
}
  • 写回答

1条回答 默认 最新

  • duancan8382 2015-05-09 21:47
    关注

    You need to change here only:--

    if(mysqli_num_rows($query) > 0){
         while ($row = mysqli_fetch_assoc($query)){
    
           if(isset($row['username']) && !empty($row['username']) && $_POST['username'] ==$row['username'] )){
              echo "Username already exist";
            }
            if(isset($row['password']) && !empty($row['password']) && $_POST['password'] ==$row['password'] )){
             echo "Password already exist";
            }
        }
    }
    

    Note:- if your password have some encryption then you need to change password part condition accordingly (just third part of it). Also if i missed any wher indexes in writing let me know.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?