0

I want to save form data to database. I get a success message in the url. But the data get not saved to the database. mysqli_stmt_execute($stmt); seems to get not executed. Can anyone explain me the below code error?

if (isset($_POST['register-submit'])) {
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $type = $_POST['utype'];

    $sql = "INSERT INTO `users`(`idroles`, `user_email`, `first_name`, `last_name`, `password`) VALUES((SELECT `idroles` FROM `roles` WHERE `name`= $type), ?, ?, ?, ?)";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("Location../View/login_register.php");
        exit();
    } else if (mysqli_stmt_prepare($stmt, $sql)) {
        $hashpwd = password_hash($password, PASSWORD_DEFAULT);
        mysqli_stmt_bind_param($stmt, "ssss", $firstname, $lastname, $email, $hashpwd);
        mysqli_stmt_execute($stmt);
        header("Location:../View/login_register.php?success");
        exit();
    }
}
Marvin Klar
  • 1,869
  • 3
  • 14
  • 32
Poorna Senani Gamage
  • 1,246
  • 2
  • 19
  • 30
  • 1
    Possible duplicate of https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments – 04FS Apr 17 '19 at 07:08
  • And don’t do `if( !foo() ) {…} else if( foo() ) {…}`, you are needlessly calling the same function twice here. – 04FS Apr 17 '19 at 07:11
  • 1
    Your code looks fine. Read [this](https://stackoverflow.com/a/22662582/2408342) and post the sql errors in the question. – Roshana Pitigala Apr 17 '19 at 07:32

1 Answers1

0

You can check if mysqli_stmt_execute succeded by doing so:

if(mysqli_stmt_execute($stmt)) {
    header("Location:../View/login_register.php?success");
    exit();
} else {
    echo mysqli_error($conn);
    exit();
}
Marvin Klar
  • 1,869
  • 3
  • 14
  • 32