使用mysqli在“if”条件下使用“||”编写stmt和“&&”操作者

时间:2018-01-20 09:01:43

标签: php mysql mysqli

我正在学习mysqli准备好的陈述,现在我遇到了一个问题,无法摆脱它。这是注册表格,并带有“用户名”“电子邮件”和“密码”。如果数据库中已存在用户名和电子邮件,则不允许注册。我使用了post方法,代码是as,

//validating email
if(!filter_var($user_email, FILTER_VALIDATE_EMAIL) === true) {
    echo "invalid email";
    exit();
}
//valid email
else{
    //prepared query
    $query_name = "SELECT * FROM users WHERE user_name = ?";
    $query_email = "SELECT * FROM users WHERE user_email = ?";

    //prepared statements
    $stmt_name = mysqli_prepare($conn, $query_name);
    $stmt_email = mysqli_prepare($conn, $query_email);

    //if bind failure
    if((!mysqli_stmt_bind_param($stmt_name, "s", $user_name)) && (!mysqli_stmt_bind_param($stmt_email, "s", $user_email))){
         echo "bind unsuccessfull";
         exit();
           }
          else{
             //if execution fails
               if( !mysqli_stmt_execute($stmt_name) && !mysqli_stmt_execute($stmt_email)){
                   echo "stmt execution failed";
                   exit();

               }
               //else if execution success
               else{

                    $result_name = mysqli_stmt_store_result($stmt_name);
                    $result_email = mysqli_stmt_store_result($stmt_email);
                    //rows
                    $row_name = mysqli_stmt_num_rows($stmt_name);
                    $row_email = mysqli_stmt_num_rows($stmt_email);
                    echo $row_name;
                    echo $row_email;
               }
         }

}

它看起来像&&条件中的if之前的第一部分,而第二部分根本不起作用。我已经尝试在数据库中使用现有的“电子邮件”进行注册,但结果仍为0。 连接很好,有效! 任何帮助将不胜感激>

3 个答案:

答案 0 :(得分:0)

不要使用如此复杂的代码,而是使用单个查询来完成工作

//validating email
if(!filter_var($user_email, FILTER_VALIDATE_EMAIL) === true) {
    echo "invalid email";
    exit();
}
else{

    $stmt = mysqli_prepare($conn, "SELECT * FROM users WHERE user_name = ? OR user_email = ?");
    mysqli_stmt_bind_param($stmt, 'ss', $user_name, $user_email);

   if( !mysqli_stmt_execute($stmt)){
       echo "stmt execution failed";
       exit();

   }else{

        $result_name = mysqli_stmt_store_result($stmt_name);
        $result_email = mysqli_stmt_store_result($stmt_email);
        //rows
        $row_name = mysqli_stmt_num_rows($stmt_name);
        $row_email = mysqli_stmt_num_rows($stmt_email);
        echo $row_name;
        echo $row_email;
   }
}

答案 1 :(得分:0)

$ query_name =“SELECT * FROM users WHERE user_name =?OR user_email =?”; 你不需要同时检查两次条件。如果有人通过电子邮件或用户名登录,那么这个查询将检查用户名和电子邮件。

答案 2 :(得分:0)

我很想使用or条件和try/catch块一起滚动两个查询,以便在不满足条件的情况下在某些阶段抛出异常。

if( !isset( $user_email ) or !filter_var( $user_email, FILTER_VALIDATE_EMAIL ) === true ) {

    exit("invalid email");

} else {
    try{
        if( isset( $user_name, $user_email ) ){

            $sql='select `user_name`,`user_email` from `users` where `user_name` = ? or  user_email = ?'
            $stmt=$conn->prepare( $sql );

            if( $stmt ){

                $stmt->bind_param('ss', $user_name, $user_email );
                $result=$stmt->execute();

                if( $result ){

                    $stmt->store_result();

                    if( $stmt->num_rows == 1 ){
                        /* user name &/or email already exists */
                        $stmt->bind_result( $name, $email );
                        $stmt->fetch();

                        printf('whoohoo! - found user "%s" with email "%s"', $name, $email );

                    } else {
                        /* no record for username or email*/

                        /* sql query to insert new user */

                    }
                    $stmt->free_result();
                    $stmt->close();

                } else {
                    throw new Exception('sql query failed to return any results');
                }
            } else {
                throw new Exception('unable to prepare sql');
            }
        } else {
            throw new Exception('username &/or email are missing');
        }
    }catch( Exception $e ){
        echo $e->getMessage();
    }