crumb on the floor
crumb on the floor

Reputation: 25

PHP form/prepared statements bug

I am creating a user registration form and I'm having an issue with the prepared statements. If I add an exit(); in the code, the sign up won't lead to anywhere. If I don't add the exit();, the form will display the correct error messages but then dismiss them the second try around.

Also, another (noob) question, how do I implement an error message for an already registered email as well? I added $sql = "SELECT * FROM users WHERE user_name=? AND user_email=?"; and mysqli_stmt_bind_param($stmt, "ss", $name, $email); but I couldn't get the error message to display.

So far for the prepared statements issue:

else {
        $sql = "SELECT * FROM users WHERE user_name=?";
        $stmt = mysqli_stmt_init($con);
        if (!mysqli_stmt_prepare($stmt, $sql)) {
            header("Location: ../index.php?sqlerror");
            exit();
        }

        else {
        mysqli_stmt_bind_param($stmt, "s", $name);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_store_result($stmt);
        $resultCheck = mysqli_stmt_num_rows($stmt);
        if ($resultCheck > 0) {
        array_push($error_array, "Oops! This username is already taken.<br>");
        exit();
        }

Upvotes: 2

Views: 52

Answers (1)

user2915350
user2915350

Reputation: 145

you do not need the exit because you already have the header for question registered email: You can use a function num_rows, http://php.net/manual/pt_BR/mysqli-result.num-rows.php

 $mysqli = new mysqli("localhost","root", "", "tables");
 $query = $mysqli->prepare("SELECT * FROM users WHERE user_name=? AND 
  user_email=?");
 $query->execute();
 $query->store_result();

 $rows = $query->num_rows;

and make an if

if($rows>0){
 //message error
}

Upvotes: 2

Related Questions