Check to see if an email is already in the database using prepared statements

前端 未结 1 1182
栀梦
栀梦 2020-12-04 02:13

I am trying to change my code to msqli prepared statements from mysql. I am not sure how to adapt my code that currently works to check if there is an email already in the d

相关标签:
1条回答
  • 2020-12-04 02:57

    Should be something like this:

    // enable error reporting for mysqli
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    // create mysqli object
    $mysqli = new mysqli(/* fill in your connection info here */);
    
    $email = $_POST['email']; // might want to validate and sanitize this first before passing to database...
    
    // set query
    $query = "SELECT COUNT(*) FROM users WHERE email = ?";
    
    // prepare the query, bind the variable and execute
    $stmt = $mysqli->prepare($query);
    $stmt->bind_param('s', $email);
    $stmt->execute();
    
    // grab the result
    $stmt->bind_result($numRows);
    $stmt->fetch();
    
    if ($numRows) {
        echo "<p class='red'>Email is already registered with us</p>";
    } else {
        // ....
    }
    

    This link may help you as well:

    http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

    0 讨论(0)
提交回复
热议问题