I am making a registration system, and I wanted to check wether or not a Username chosen by the user has already been used and if it hasn't already been used, enter the chosen username into the DB?.
below is my query as it is so far, but I keep getting syntax errors. For the record: I am going to use BindParam() to bind the PHP input of the user into the query :)
I also have another column called "ID-number", which is a primary key.
Any help on how to do this is welcome!
Greets,
Romulus
SELECT CASE
WHEN `Username` != `Username`
THEN
INSERT INTO 'T_Users' `Username`, `Password`, `Email`
VALUES ':username', ':password', ':email'
END
FROM `T_Users`
you can do something like this
<?php
$query = mysqli_query($con , "SELECT 1 FROM `T_Users` WHERE username = '$username'");
if(mysqli_num_rows($query) > 0) {
echo "Username is already taken";
} else {
# Username is available do insert the Data into Database ....
}
?>
I'd suggest putting a unique key on the Username field and trying to do an insert. You can then catch the error and then can know that the user already exists. This is good as it isn't subject to race conditions (2 or more people trying to register at the same time with the same username and both successfully getting the same username).
Try something like:
if ($mysqli->query("INSERT INTO `T_Users` (Username, Password, Email) VALUES('xxx', 'xxx', 'xxx')") === TRUE){
echo 'Welcome to the site!';
} else {
echo 'Username taken';
}