PDO :: execute总是返回FALSE,但查询是succwssful

时间:2017-08-22 11:38:12

标签: if-statement pdo sql-insert execute

我试图获得有关DB中已保存行的确认,但我的条件总是返回FALSE。

$conn = new PDO($dir) or die("hovno");
$query = "INSERT INTO sncheck (caseId, snPaper, snBox, snUnit) VALUES ('$caseId', '$snPaper', '$snBox', '$snUnit')";
$prep = $conn->prepare($query);
if($prep->execute()){
    print "DONE";
}
else{
    print "ERROR";
}

它始终触发else语句。

2 个答案:

答案 0 :(得分:1)

更改为:

$req = $conn->prepare("INSERT sncheck users (caseId, snPaper, snBox, insnUnitos, roles_id) VALUES (:login, :password, :email, :infos, :snUnit)")or exit(print_r($conn->errorInfo()));
    $req->execute(array(
            "caseId" => $caseId, 
            "snPaper" => $snPaper,
            "snBox" => $snBox,
            "nUnit" => $nUnit
            ));

答案 1 :(得分:0)

尝试调试您的查询,如下所示,这样您就会收到正确的错误消息 -

    $conn = new PDO($dir) or die("hovno");
    $query = "INSERT INTO sncheck (caseId, snPaper, snBox, snUnit) VALUES ('$caseId', '$snPaper', '$snBox', '$snUnit')";
    $prep = $conn->prepare($query);
    if (!$prep) {
        echo "\nPDO::errorInfo():\n";
        print_r($conn->errorInfo());
    }

    if($prep->execute()){
        print "DONE";
    }else{
        echo "\nPDO::errorInfo():\n";
        print_r($conn->errorInfo());
        print "ERROR";
    }

如果仍有问题,请告诉我,并提供错误消息。