1

I have been stumped by this error i am trying validate user that if username or email is already in the table.please help....

function checkDuplicateEntries($table,$column_name,$value,$db){

   try {

       $sqlQuery = "SELECT * FROM " .$table."WHERE".$column_name."=:$column_name";

       $statement = $db->prepare($sqlQuery);

       $statement->execute(array(':$column_name'=> $value));

       if($row=$statement->fetch()){

           return true;
       }
       return false;

   }catch (PDOException $ex){

       echo"error while checking for duplicate entries".$ex->getMessage();

   }


 }


 if(checkDuplicateEntries("users","email",$email,$db)){

            $result = flashMessage("Email is already taken please try another one");
        }


else if(checkDuplicateEntries("users","username",$username,$db)){

        $result = flashMessage("Username is already taken please try another one");
    }
u_mulder
  • 54,101
  • 5
  • 48
  • 64
Tariq
  • 23
  • 5

1 Answers1

0

The reason is that you use single quotes on execute() call:

$statement->execute(array(':$column_name'=> $value));

Switch it to a double quotes and try again. Please also take a look at: What is the difference between single-quoted and double-quoted strings in PHP?

Community
  • 1
  • 1
deniskoronets
  • 520
  • 3
  • 15
  • In your situation in the `$sqlQuery` condition you use double quotes, so `$column_name` value will be extracted. But then you use single quotes on execute, so value will be not extracted and pdo will see `:$column_name`, not value of that variable. Please attentively read info on the link that I provided, and you have no this issue again. – deniskoronets Aug 13 '16 at 08:46