I am having issues denying the registration of existing users. please see code below, and further details below that.
<?php
//connect to sql
$con=mysqli_connect("host","user","pass","db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//pull user id from form input
$uid=$_POST[uid];
//make sure input is not blank
if($uid==""){echo 'Please enter a username.';exit();}
//make sure it is alphanumeric input only
if(!ctype_alnum($uid)){echo 'The username chosen must be alpha-numberic (A-z,0-9),
without special characters. Please use the back button in your
browser to try again using only alpha-numeric input.';exit();}
//check if user exists
$query=mysqli_query($con, "SELECT * FROM user WHERE uid='$uid'");
if(!$query){die 'query failed to execute';}
if(mysqli_num_rows($query)!=0){echo 'user already exists. Use back button and enter a new username'; exit();
}
//insert record into DB
$sql= "INSERT INTO user (uid, addr, password, passwordlastchanged, email)
VALUES
('$uid', '$_POST[addr]', '$_POST[pwd]','today', '$_POST[email]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
So when I input through my form it works fine, its just that if I try to register an already existing username, it allows it, when I have setup the query to look for an existing entry and cancel if it finds it (or so i thought...).
can you see what I am doing wrong? again I want it to check for the user name and deny further action if it finds it..
I know this is probably lacking in security, I am unfortunately learning as I go so just getting things to work comes first :) Although security suggestions will be logged and considered at the right time.
I spent an enormous amount of time researching this and I feel like I've tried a lot of various query/ function combos.