The behavior I expect is that a query is first ran on the database to determine if a username or email already exist on there. If they do, skip if((count((array)$result) == 0))
because the result will either be 1 or 2 if a username or email is already present or 0 if neither are.
However, the if statement is being skipped even if both values are unique and else { echo ('Username or Email already exists!');}
is being executed.
Similarly phrased questions I have checked but did not find a solution here and here
if(isset($_POST['submit']))
{
$username = !empty($_POST['username']) ? trim($_POST['username']) : null;
$email = !empty($_POST['email']) ? trim($_POST['email']) : null;
//other variables remove for brevity, but they are all formed the exact same
$query = $pdo->prepare("SELECT * FROM users WHERE username= ? OR email= ? LIMIT 2");
$query->bindParam(1, $username, PDO::PARAM_STR);
$query->bindParam(2, $email, PDO::PARAM_STR);
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC);
if ((count((array)$result) == 0))
{
$query = $pdo->prepare ("INSERT INTO users (firstname, lastname, username, password, email, address, city, province, postalcode) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
$query->bindValue(1, $fname, PDO::PARAM_STR);
$query->bindValue(2, $lname, PDO::PARAM_STR);
$query->bindValue(3, $username, PDO::PARAM_STR);
$query->bindValue(4, $password, PDO::PARAM_STR);
$query->bindValue(5, $email, PDO::PARAM_STR);
$query->bindValue(6, $address, PDO::PARAM_STR);
$query->bindValue(7, $city, PDO::PARAM_STR);
$query->bindvalue(8, $province, PDO::PARAM_STR);
$query->bindValue(9, $postalcode, PDO::PARAM_STR);
$query->execute();
echo 'Registration Successful!';
}
else { echo ('Username or Email already exists!');}
}
//closing the database after execution
$query = null;
$pdo = null;
For example SELECT * FROM users WHERE (username='kanas' OR email='[email protected]');
returns zero rows as expected in PHPMyAdmin. But when ran through my html whats echoed back is "Username or Email already exists!"
The create table:
CREATE TABLE `users` (
`userID` int(11) NOT NULL,
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`lastlogin` date NOT NULL,
`permission` int(3) NOT NULL,
`email` varchar(320) NOT NULL,
`address` varchar(50) NOT NULL,
`city` varchar(50) NOT NULL,
`province` varchar(50) NOT NULL,
`postalcode` varchar(6) NOT NULL,
`userrequests` text NOT NULL
);
2
Answers
You can try this
If no rows were found then
fetch()
will return false. When you docount((array)false)
the result will always be 1.To fix the problem simply check if
fetch()
returned anything.However, be warned that what you currently have will not prevent duplicate records. To prevent duplicate records you would have to add UNIQUE constraint in the database. See How to prevent duplicate usernames when people register?