PDO准备的语句插入数据失败

时间:2019-04-15 00:34:17

标签: php pdo

我正在将数据插入MS SQLSERVER。我正在使用PDO插入我的数据。我在MySQL和MS SQL中都检查了查询,并且运行良好,但是当我使用pdo代码时,我得到的ErrorInfo为“ 00000”。这是什么?有没有办法完全避免或消除这个问题?我一直在寻找答案,但提供给我的解决方案无法正常工作。

我尝试过的事情:
1.我尝试添加 $ conn-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
2.我尝试将插入查询封装在try catch中仍然无法正常工作

$sql = $conn->prepare("SELECT ContactID FROM tblUsers WHERE UserID = :userid", $cursor);
$sql->bindValue(":userid", $Username);

if($sql->execute()){
    $row = $sql->fetch();

    $getserialsql = $conn->prepare("SELECT * FROM tblSystemSerial WHERE SerialNumber = :code AND Trials = '1' AND ContactID = :contactid", $cursor);
    $getserialsql->bindValue(":code", $RegistrationCode);
    $getserialsql->bindValue(":contactid", $row["ContactID"]);

    if($getserialsql->execute()){
        $getserialcount = $getserialsql->rowCount();

        if($getserialcount == 0){
            $subscriptionsql = $conn->prepare("INSERT INTO tblSystemSerial(SerialNumber, ContactID, DateStart, NoOfDays, Trials, InputSerialNumber, Deleted, ClientUpdate, ServerUpdate) VALUES(:code, :contactid, :datestart, :noofdays, :trials, :inputserial, :deleted, :clientupdate, :serverupdate)", $cursor);

            $subscriptionsql->bindValue(":code", $RegistrationCode);
            $subscriptionsql->bindValue(":contactid", $row["ContactID"]);
            $subscriptionsql->bindValue(":datestart", date("Y-m-d"));
            $subscriptionsql->bindValue(":noofdays", $encdays);
            $subscriptionsql->bindValue(":trials", '1');
            $subscriptionsql->bindValue(":inputserial", $encr);
            $subscriptionsql->bindValue(":deleted", 0);
            $subscriptionsql->bindValue(":clientupdate", date("Y-m-d h:i:s"));
            $subscriptionsql->bindValue(":serverupdate", '01-01-0001');

            if($subscriptionsql->execute()){
                $ar[] = array(
                    'Message' => "Inserted",
                    'ContactID' => $row["ContactID"]
                );
            }
            else{
                $ar[] = array(
                    "Message" => $sql->errorInfo()
                );
            }
        }
    }
    else{
        $ar[] = array(
            "Message" => $sql->errorInfo()
        );
    }
}
else{
    $ar[] = array(
        "Message" => $sql->errorInfo()
    );
}

 print json_encode($ar);$sql = $conn->prepare("SELECT ContactID FROM tblUsers WHERE UserID = :userid", $cursor);
$sql->bindValue(":userid", $Username);

if($sql->execute()){
    $row = $sql->fetch();

    $getserialsql = $conn->prepare("SELECT * FROM tblSystemSerial WHERE SerialNumber = :code AND Trials = '1' AND ContactID = :contactid", $cursor);
    $getserialsql->bindValue(":code", $RegistrationCode);
    $getserialsql->bindValue(":contactid", $row["ContactID"]);

    if($getserialsql->execute()){
        $getserialcount = $getserialsql->rowCount();

        if($getserialcount == 0){
            $subscriptionsql = $conn->prepare("INSERT INTO tblSystemSerial(SerialNumber, ContactID, DateStart, NoOfDays, Trials, InputSerialNumber, Deleted, ClientUpdate, ServerUpdate) VALUES(:code, :contactid, :datestart, :noofdays, :trials, :inputserial, :deleted, :clientupdate, :serverupdate)", $cursor);

            $subscriptionsql->bindValue(":code", $RegistrationCode);
            $subscriptionsql->bindValue(":contactid", $row["ContactID"]);
            $subscriptionsql->bindValue(":datestart", date("Y-m-d"));
            $subscriptionsql->bindValue(":noofdays", $encdays);
            $subscriptionsql->bindValue(":trials", '1');
            $subscriptionsql->bindValue(":inputserial", $encr);
            $subscriptionsql->bindValue(":deleted", 0);
            $subscriptionsql->bindValue(":clientupdate", date("Y-m-d h:i:s"));
            $subscriptionsql->bindValue(":serverupdate", '01-01-0001');

            if($subscriptionsql->execute()){
                $ar[] = array(
                    'Message' => "Inserted",
                    'ContactID' => $row["ContactID"]
                );
            }
            else{
                $ar[] = array(
                    "Message" => $sql->errorInfo()
                );
            }
        }
    }
    else{
        $ar[] = array(
            "Message" => $sql->errorInfo()
        );
    }
}
else{
    $ar[] = array(
        "Message" => $sql->errorInfo()
    );
}

 print json_encode($ar);

0 个答案:

没有答案