MattClaff MattClaff - 25 days ago 15
MySQL Question

Check if username exists in a database

pretty stuck on trying to prevent the 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 have tried loads of other stuff to get it working but for some reason my code doesn't like me......

Answer

change mysql_num_rows to mysqli_num_rows

if(mysqli_num_rows($result)>=1)//You are mixing the mysql and mysqli, change this line of code
           {
            echo"name already exists";
           }
         else
            {  //

use bind param or proplerly escape your value, before using it in query.

Comments