I'm working on log-in/register form for school and i've encountered a problem.
How do I give an error message if you use a username that already exists on the database (I've used the UNIQUE KEY for the usernames)
Thanks in advance for any answers :) have a nice day
<?php
    error_reporting(E_ALL & ~E_NOTICE);
    session_start();
    if ($_POST['submit']) {
        // check if passwords match
        if ($_POST['password'] == $_POST['confirmpassword']) {
            include_once("connect.php");
            $username = $dbCon->real_escape_string($_POST['username']);
            $password = ($_POST['password']); 
            $sql = "INSERT INTO members (username, password, activated) "
                    ."VALUES ('$username', '$password', '1')";
            // if registration succesful, send to user.php, if not, error message
            if ($dbCon->query($sql) === true) {
                header('Location: user.php');
                echo "Registration succesful, Welcome $username";
                exit;
            } else {
                echo "Could not register, please fill in both username and password";
            }
        } else { // if the passwords do not match
            echo "Lösenorden matchar inte!";
        }
  }
?>
 
    