PHP PDO插入或更新测试

时间:2016-07-01 02:31:24

标签: php mysql pdo

我有下面的代码,但由于某种原因它似乎都回显“记录存在”并仍然在表中插入一个新行。我究竟做错了什么?

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']
        ));
}


}

0 个答案:

没有答案