0

I have a small problem with an email validation. I want to check in the database if the email already exists but I always get the "Oops! Something went wrong. Please try again later.", path. Does anyone see an error that I do not see? I would appreciate your help!

//Validate email 
if(empty(trim($_POST["email"]))) {
    $email_err = "Please enter a emailadress.";
} else{
    // Prepare a select statement
    $sql = "SELECT id FROM users WHERE email = ?";
    
    if($stmt = mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "s", $param_email);
        
        // Set parameters
        $param_email = trim($_POST["email"]);
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            /* store result */
            mysqli_stmt_store_result($stmt);
            
            if(mysqli_stmt_num_rows($stmt) == 1){
                $email_err = "This email is already taken.";
            } else{
                $email = trim($_POST["email"]);
            }
        } else{
            echo "Oops! Something went wrong. Please try again later.";
        }

        // Close statement
        mysqli_stmt_close($stmt);
    }
}
allstar_141
  • 43
  • 1
  • 1
  • 8
  • Rather than outputting "oops", you should output the error messages. https://www.php.net/manual/en/mysqli-stmt.error-list.php Or use https://www.php.net/manual/en/mysqli-driver.report-mode.php to make errors cause a PHP error/exception. – ceejayoz Jun 03 '22 at 16:13
  • @ceejayoz now that's funny. I have nothing, really nothing changed and suddenly it works. I've been sitting on this problem for an hour. I just don't get it... – allstar_141 Jun 03 '22 at 16:18
  • Might be as simple as "your database server was down". Hard to say if you don't output errors. Always have error handling in your code; `mysqli_report(MYSQLI_REPORT_ERROR);` will make any of your SQL calls throw errors if they have them. – ceejayoz Jun 03 '22 at 16:37

0 Answers0