0

I have the code below, yet for some reason it seems to both echo "The record exists" and still insert a new row into the table. What am I doing wrong?

if(isset($_POST['name'])){

  try {
        $stmt = $db->prepare('SELECT `username` FROM `sites` WHERE name = ? AND username = ?');
        $stmt->bindParam(1, $_POST['name']); 
        $stmt->bindParam(2, $loggeduser); 
        $stmt->execute();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

        }
    }
    catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }

if($stmt->rowCount() > 0){
    echo "The record exists!";
} else {
   $stmt = $db->prepare("INSERT INTO sites (username,name,affiliate,banner,login,user,pass,category,description) VALUES (:username,:name,:affiliate,:banner,:login,:suser,:spass,:category,:description)");
$stmt->execute(array(
            ':username' => $loggeduser,
            ':name' => $_POST['name'],
            ':affiliate' => $_POST['affiliate'],
            ':banner' => $_POST['banner'],
                            ':login' => $_POST['login'],
            ':suser' => $_POST['user'],
            ':spass' => $_POST['pass'],
                            ':category' => $_POST['category'],
                            ':description' => $_POST['description']
        ));
}


}
Bruce
  • 1,039
  • 1
  • 9
  • 31

0 Answers0