I have been working on a site in PHP and SQL. In my last question some one pointed out to me that I should be using prepared statements in order to protect my site against SQL injections. I have been going through my site changing all my queries to prepared statements, however I am having a problem with checking if a username exist in the database when a user signs up.
Here is the code that I have tried, it does not produce any errors, however it allows users to sign up with usernames that are already in my users table.
$uname = $_POST['uname'];
$stmt = $link->prepare("SELECT * FROM users WHERE username= ?");
/* Bind parameters, s - string, b - blob, i - int, etc */
$stmt -> bind_param("s", $uname);
$stmt -> execute();
/* Fetch the value */
$stmt -> fetch();
$numberofrows = $stmt->num_rows;
/* Close statement */
$stmt -> close();
if($numberofrows>0) {
echo "Username Already Exist";
} else {
//rest of my code
}
Thanks in advance!