Check if username exists in the database
pretty stuck on trying to prevent a user from registering if the username exists. Here is my code:
include('../connection.php');
//get all the names and values that have been posted.
$username = $_POST['username'];
//check if username exists
$sql = "SELECT * FROM tbl_Freelancers WHERE User_Username = '".$username."'";
$result = mysqli_query($con,$query);
if(mysql_num_rows($result)>=1)
{
echo"name already exists";
}
else
{ // excecute insert query
I tried a lot of other things to make it work, but for some reason I don't like my code ......
+3
source to share
6 answers
change..
$sql = "SELECT * FROM tbl_Freelancers WHERE User_Username = '".$username."'";
$result = mysqli_query($con,$query);
to
$sql = "SELECT * FROM tbl_Freelancers WHERE User_Username = '".$username."'";
$result = mysqli_query($con,$sql);
you specified the query in $ sql and executed in $ query ... also get input as ..
$username = mysqli_real_escape_string($con, $_POST['username']);
for security reasons ...
+2
source to share