-2

The error:

Fatal error: Uncaught Error: Call to a member function close() on boolean in C:\xampp\htdocs\Project\script\register.php:69

Stack trace: #0 C:\xampp\htdocs\Project\index.php(2): include_once() #1 {main} thrown in C:\xampp\htdocs\Project\script\register.php on line 69

The error occurred when i passed proper data that gets through the validation.

The file :

<?php 
include_once 'functions.php';
include_once 'dbconnect.php';
// Returns the request method used to access the page
if ($_SERVER["REQUEST_METHOD"] == "POST"){
    $err="";


// Validation .... 
 .....
// End of it

 $prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1";
$stmt = $sql_db->prepare($prep_stmt);

// check existing email  
if ($stmt) {
    $stmt->bind_param('s', $email);
    $stmt->execute();
    $stmt->store_result();

    if ($stmt->num_rows == 1) {
        // A user with this email address already exists
        $err .= '<p class="error">A user with this email address already exists.</p>';
                    $stmt->close();
    }
} else {
    $err .= '<p class="error">Database error Line 39</p>';
            $stmt->close();
}

// check existing username
$prep_stmt = "SELECT id FROM members WHERE username = ? LIMIT 1";
$stmt = $sql_db->prepare($prep_stmt);

if ($stmt) {
    $stmt->bind_param('s', $username);
    $stmt->execute();
    $stmt->store_result();

            if ($stmt->num_rows == 1) {
                    // A user with this username already exists
                    $err .= '<p class="error">A user with this username already exists</p>';
                    $stmt->close();
            }
    } else {
            $err .= '<p class="error">Database error line 55</p>';
            $stmt->close();
    }

// Hashing, salting and inserting the values.
   ....
// End of file.

Searched for some answer why is there a problem and nothing found.

Another thread like this - The $stmt->close() is in an if function.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Think about it - you run `if ($stmt)`...so if that fails and goes into the `else` then it must mean that `$stmt` was `false`...i.e. a **boolean** just like the error message said. So how you you then call a method (->close() in your case) on a boolean? Trivially we can see that it makes no sense. (And if the statement failed, there's nothing to close anyway....so you just don't need that line). And then next you need to find out _why_ the statement failed. – ADyson Dec 18 '18 at 12:05

1 Answers1

0
if ($stmt) {
    $stmt->bind_param('s', $email);
    $stmt->execute();
    $stmt->store_result();

    if ($stmt->num_rows == 1) {
        // A user with this email address already exists
        $err .= '<p class="error">A user with this email address already exists.</p>';
                    $stmt->close();
    }
} else {
    $err .= '<p class="error">Database error Line 39</p>';
    // Remove this. $stmt does not exist here due to your if/else condition
    //$stmt->close();  
}
BadHorsie
  • 14,135
  • 30
  • 117
  • 191
  • "$stmt does not exist"...not strictly true. You're right in the sense that it isn't a mysqli Statement object. But actually `$stmt` is `false` (hence the reference to a boolean in the error message) – ADyson Dec 18 '18 at 12:09
  • @ADyson Sure, but if OP doesn't understand this basic logic, it's not really worth going into that much detail. – BadHorsie Dec 18 '18 at 12:14
  • OTOH there's equally no reason to give them slightly inaccurate information which might confuse them further...especially as it relates _directly_ to the error message they're seeing - the error talks about a boolean, not about an undefined variable. You can spend two seconds correcting your comment and then your question is 100% accurate. Understanding code is all about the precise detail - that's my point really. Something being `false` and something not existing are not the same state. – ADyson Dec 18 '18 at 12:17