-1

I making codes in PHP and I had met some error, it just skip my else statement also

<?php
// put your code here


//    $select_query = "Select * from `user_table` where username='$user_username'";
//    $result = mysqli_query($con, $select_query);
    $sql = "Select * from `user_table` where username= ? "; // SQL with parameters
$stmt = $con->prepare($sql); 
$stmt->bind_param("s", $user_username);
$stmt->execute();
$result = $stmt->get_result();
$row_count = mysqli_num_rows($result);
$row_data = mysqli_fetch_assoc($result);
$user_ip = getIPAddress();


if ($_SESSION['attempt'] == 5) {
    $_SESSION['error'] = 'Attempt limit reach';
} else {
         
    if ($row_count > 0) {
        $_SESSION['username'] = $user_username; 
        $row = mysqli_fetch_object($result);
        if (password_verify($user_password, $row_data['user_password'])) {
                
             
//                if ($row_count == 1 and $row_count_cart == 0) {
//
//                    echo"<script>alert('Login Successfully')</script>";
//                    echo"<script>window.open('profile.php','_self')</script>";
//                } else {
//                    $_SESSION['username'] = $user_username;
//                    echo"<script>alert('Login Successfully')</script>";
//                    echo"<script>window.open('payment.php','_self')</script>";
//                }
                if ($row->is_tfa_enabled)
                {
                    $row->is_verified = false;
                    $_SESSION["username"] = $row;
 
                    $pin = rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9);
                     
                    $sql = "UPDATE user_table SET pin = '$pin'  WHERE user_id = '" . $row->user_id . "'";
                    mysqli_query($con, $sql);
 

 
                    header("Location: enter_pin.php");
                } else {
                    $row->is_verified = true;
                    $_SESSION["username"] = $row;
 
                    header("Location: profile.php");
                }
            } else {
                
                echo"<script>alert('Invalid Credentials(Password Incorrect) " .$number ." attempt left  ') </script>";
              
                
              
                $_SESSION['error'] = 'Password incorrect  ';
                //this is where we put our 3 attempt limit
                $_SESSION['attempt'] += 1;
                //set the time to allow login if third attempt is reach
                if ($_SESSION['attempt'] == 5) {
                    $_SESSION['attempt_again'] = time() + (1 * 60);
                    //note 5*60 = 5mins, 60*60 = 1hr, to set to 2hrs change it to 2*60*60
                }
                 
            }
        } else {
            echo"<script>alert('Invalid Credentials')</script>";
        }
    }
}
?>

so when I run the code it shows 2 error which is

1)Warning: Attempt to read property "is_tfa_enabled" on null in C:\xampp\htdocs\FinalYearProject\Users\user_login.php on line 148

2)Fatal error: Uncaught Error: Attempt to assign property "is_verified" on null in C:\xampp\htdocs\FinalYearProject\Users\user_login.php:170 Stack trace: #0 {main} thrown in C:\xampp\htdocs\FinalYearProject\Users\user_login.php on line 170

when i change the line 148 into $row_data['is_tfa_enabled'] then the error 1 will solve

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
CHOONG
  • 1
  • 1
  • 2
    1) `is_tfa_enabled` is not found on `$row` variable as it probably is `null`. From the docs of `mysqli_fetch_object()` it says that "null if there are no more rows in the result set". //// 2) Because your `$row'` is null, also here a property can not be assigned. You should therefore debug `$row`. Welcome to StackOverflow, please read for the next time https://stackoverflow.com/help/minimal-reproducible-example – WebDevPassion Dec 11 '22 at 16:30
  • What have you tried to resolve the problem? Where are you stuck? – Nico Haase Dec 11 '22 at 18:51
  • You are attempting to read two rows from the mysqli_result but it seems you only have one row. Why do you call `mysqli_fetch_object` after calling `mysqli_fetch_assoc`? – Dharman Dec 11 '22 at 19:26

1 Answers1

-2

The is_tfa_enabled and is_verified rows are not found on the table also $row->is_verified = true; is not a valid commande . You can't change column value just like this