Trying to check if username already exists in MySQL database using PHP

后端 未结 8 1843
无人共我
无人共我 2020-12-28 10:12

I\'ve looked at the many other posts that were similar to my issue and implemented their solutions (as far as I can tell) as exactly as I could. However, every time I execut

相关标签:
8条回答
  • 2020-12-28 10:52
    $firstname = $_POST["firstname"];
    $lastname = $_POST["lastname"];
    $email = $_POST["email"];
    $pass = $_POST["password"];
    
    $check_email = mysqli_query($conn, "SELECT Email FROM crud where Email = '$email' ");
    if(mysqli_num_rows($check_email) > 0){
        echo('Email Already exists');
    }
    else{
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $result = mysqli_query($conn, "INSERT INTO crud (Firstname, Lastname, Email, Password) VALUES ('$firstname', '$lastname', '$email', '$pass')");
    }
        echo('Record Entered Successfully');
    }
    
    0 讨论(0)
  • 2020-12-28 11:04

    $query = mysql_query("SELECT username FROM Users WHERE username='$username' ")

    Use prepared statements, do not use mysql as it is deprecated.

    // check if name is taken already
    $stmt = $link->prepare("SELECT username FROM users WHERE username = :username");
    $stmt->execute([
        'username' => $username
    ]);
    $user = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (isset($user) && !empty($user)){
        // Username already taken
    }
    
    0 讨论(0)
提交回复
热议问题