-1

I have an SQL table table taking care of the members list of my website. However, when registering a new user, the data is not inserted in the table. No error is being shown either as I check for them a few times throughout my code.

if (empty($error_msg)) {

        // Create hashed password using the password_hash function.
        // This function salts it with a random salt and can be verified with
        // the password_verify function.
        $password = password_hash($password, PASSWORD_BCRYPT);

        // Insert the new user into the database 
        if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password) VALUES (?, ?, ?)")) {
            $insert_stmt->bind_param('sss', $username, $email, $password);

            // Execute the prepared query.
            if (!$insert_stmt->execute()) {
                header('Location: ./error.php?err=Registration failure: INSERT');
            }

            header('Location: ./register_success.php?');
        }
    }

The code executes to the end and I am redirected to register_success. I know it's not because I have entered the wrong data base credentials as somewhere ele in my code, I am sending data to the same database with the same credentials(they're kept in another file) and it works perfectly fine.

EDIT: After removing the second "header" as Nick Coons said, I'm getting redirected to the error page meaning there's a problem with the execute. Still, I've got no idea why...

Zerox029
  • 43
  • 1
  • 10
  • 2
    Where's the code that checks for displays any errors? Also, it's going to redirect to register_success.php whether there's an error or not, because your second `header()` function is going to execute regardless, and will overwrite the first. – Nick Coons Aug 17 '17 at 23:32
  • @NickCoons Oh, I didn't know the second one would overwrite the first. Then, in that case, I just removed the second one and I'm redirected to the error page meaning there's a problem with the execute. Still, I have no idea why. – Zerox029 Aug 17 '17 at 23:39
  • Have a look at this to output the error that's occurring: http://php.net/manual/en/mysqli.error.php – Nick Coons Aug 17 '17 at 23:41
  • That fixed it thanks. It's just that one of the values in the table was never assigned. Could you put your comment as an answer so I can accept you answer? – Zerox029 Aug 17 '17 at 23:45
  • You got it, thanks! – Nick Coons Aug 18 '17 at 00:00

1 Answers1

1

You'll want to use the documentation on this page to determine what the SQL error is, if there is one.

Additionally, you're executing the header() function twice if there is an error, and the second instance of it will overwrite the first one (because you can only send one "Location" header), so it will redirect to your success page no matter what. I would recommend putting the second header() call in an else clause of your if statement, or adding exit() after your first call to header() so that only one or the other executes.

Nick Coons
  • 3,682
  • 1
  • 19
  • 21