I am trying to build a database for a website that allows users to login if they have already created an account, or create an account then login. The problem I am currently running into is that I want only unused user names to be added to the database and used ones to be rejected. My mySQL query seems to work, either putting back an empty set if the userInput isn't in the set. Currently, if you input any username, regardless of it being taken or not, it will not let you in. Below is my code:
if($firstName != null && $address != null)
{
$userCheck =$dbh->query("SELECT userID FROM user WHERE userID='$userInput'");
if(!$userCheck)
{
$sql = "INSERT INTO user(firstName, userID, password, address) VALUES ('$firstName', '$userInput', '$password', '$address')";
$dbh->exec($sql);
is_password_correct($userInput, $password);
}
}
Essentially... if the first two fields are filled our (or not null) then the query wil run, and if that is empty then server should recieve the information and allow the user to access the page.
function is_password_correct($userInput, $password)
{
$dbh = new PDO
$userInput = $_POST["userID"];
$password = $_POST["password"];
$firstName = $_POST["first"];
$rows =$dbh->query("SELECT password FROM user WHERE userID='$userInput' ");
if($rows)
{
foreach ($rows as $row)
{
if($password == $row["password"])
{
return TRUE;
}
}
}
return FALSE;
}