-1

I am having a trouble when i try to add a data to MySQL database. I click the submit button and nothing happens just the page is being refreshed.

The following error occurs:

Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, bool given in C:\xampp\htdocs\loginsystm\register.php on line 92

Here's my PHP code:

    if(empty($name) && empty($password) && empty($confirm_password) && empty($thirdName) && empty($phone) && empty($email)){

    // Prepare an insert statement
    $sql = "INSERT INTO users (username, thirdName, email, phone, age, password) VALUES ('$name', '$password', '$thirdName','$email','$phone')";
    if($stmt = mysqli_prepare($conn,$sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "sssss", $name, $password, $thirdName,$email,$phone);

        // Set parameters
        $param_name = $name;
        $param_thirdName = $thirdName;
        $param_email = $email;
        $param_phone = $phone;
        $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash

        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            // Redirect to login page
            header("location: login.php");
        } else{
            echo "Something went wrong. Please try again later.";
        }
    }

    // Close statement
    mysqli_stmt_close($stmt);
}

And here is my HTML

       <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
        <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
            <label>Name</label>
            <input type="text" name="name" class="form-control" value="<?php echo $name; ?>">
            <span class="help-block"><?php echo $name_err; ?></span>
        </div>    
        <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
            <label>Third Name</label>
            <input type="text" name="thirdName" class="form-control" value="<?php echo $password; ?>">
            <span class="help-block"><?php echo $password_err; ?></span>
        </div>
        <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
            <label>Email</label>
            <input type="text" name="email" class="form-control" value="<?php echo $confirm_password; ?>">
            <span class="help-block"><?php echo $confirm_password_err; ?></span>
        </div>
        <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
            <label>Phone number</label>
            <input type="text" name="phone" class="form-control" value="<?php echo $confirm_password; ?>">
            <span class="help-block"><?php echo $confirm_password_err; ?></span>
        </div>
        <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
            <label>Password</label>
            <input type="password" name="password" class="form-control" value="<?php echo $confirm_password; ?>">
            <span class="help-block"><?php echo $confirm_password_err; ?></span>
        </div>
        <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
            <label>Confirm Password</label>
            <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
            <span class="help-block"><?php echo $confirm_password_err; ?></span>
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-primary" value="Submit">
            <input type="reset" class="btn btn-default" value="Reset">
        </div>
        <p>Already have an account? <a href="login.php">Login here</a>.</p>
    </form>

I know my html looks so bad but i don't have the time to manage it i'm sorry. Thank you!

MaartenDev
  • 5,631
  • 5
  • 21
  • 33
  • Does this answer your question? [mysqli\_stmt\_close() expects parameter 1 to be mysqli\_stmt, boolean](https://stackoverflow.com/questions/53459523/mysqli-stmt-close-expects-parameter-1-to-be-mysqli-stmt-boolean) – MaartenDev Jan 18 '20 at 11:25

1 Answers1

0

The very first line of your code:

if(empty($name) && empty($password) && empty($confirm_password) && empty($thirdName) && empty($phone) && empty($email)){

That condition is true if ALL parameters are empty. Since you're submitting the form, at least one of them is not empty, so the condition is false.


Also, you're not using prepared statements correctly:

    $sql = "INSERT INTO users (username, thirdName, email, phone, age, password) VALUES ('$name', '$password', '$thirdName','$email','$phone')";

Variables are encapsulated inside the SQL query, which is a security vulnerability named SQL injection.

You should also call exit() after your call to the header function.

Benoit Esnard
  • 2,017
  • 2
  • 24
  • 32