如何解决不起作用的PDO对象并且不会出错?

时间:2013-01-16 14:35:58

标签: php class object pdo

再次使用object / classes。我有一个名为submitticket的函数,它会在我的票证表中提交一个新行。

public function submitticket() {
    $correct = false;
    try {
        $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
        $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $sql = "INSERT INTO tickets (ticketid, EPMSAccount, requester, recipient, jobnumber, subject, body, responseid, type, priority) VALUES (:ticketid, :EPMSAccount, :requester, :recipient, :jobnumber, :subject, :body, :responseid, :type, :priority)";
        $stmt = $con->prepare( $sql );
        //print_r($sql);
        $stmt->bindValue( "ticketid", $this->ticketid, PDO::PARAM_STR );
        $stmt->bindValue( "EPMSAccount", $this->EPMSAccount, PDO::PARAM_STR );
        $stmt->bindValue( "requester", $this->requester, PDO::PARAM_STR );
        $stmt->bindValue( "recipient", $this->recipient, PDO::PARAM_STR );
        $stmt->bindValue( "jobnumber", $this->jobnumber, PDO::PARAM_STR );
        $stmt->bindValue( "subject", $this->subject, PDO::PARAM_STR );
        $stmt->bindValue( "body", $this->body, PDO::PARAM_STR );
        $stmt->bindValue( "responseid", $this->responseid, PDO::PARAM_STR );
        $stmt->bindValue( "type", $this->type, PDO::PARAM_STR );
        $stmt->bindValue( "priority", $this->priority, PDO::PARAM_STR );
        echo '<pre>';
        print_r($stmt);
        echo '</pre>';
        $stmt->execute();


        return "Ticket created!";

    }catch( PDOException $e ) {
        return $e->getMessage();
    }

这就是我在我的代码中执行它的方式:

// Submit ticket if submit clicked
if($_POST['submit'] == 'Submit Ticket'){
    $_POST['requester'] = $_SESSION['name'];
    $_POST['EPMSAccount'] = $_SESSION['EPMSAccount'];
    $tkt = new Ticket; // New instance of ticket class
    $tkt->storeFormValues( $_POST );
    $tkt->storeFormValues( $_SESSION );
    $tkt->submitticket();
    echo '<pre>';
    var_dump($tkt);
    print_r ($stmt);
    echo '</pre>';

} else {
echo 'nothing attempted';
}

我知道这个查询应该是正确的,但它不会写任何东西给我的表。这是var_dump和print_r给我的内容:

PDOStatement Object
(
[queryString] => INSERT INTO tickets (ticketid, EPMSAccount, requester, recipient, jobnumber, subject, body, responseid, type, priority) VALUES (:ticketid, :EPMSAccount, :requester, :recipient, :jobnumber, :subject, :body, :responseid, :type, :priority)
)
object(Ticket)#2 (12) {
["ticketid"]=>
NULL
["EPMSAccount"]=>
string(7) "SHAWMUT"
["jobnumber"]=>
string(6) "123456"
["estnumber"]=>
NULL
["requester"]=>
string(14) "Gabriel Peluso"
["recipient"]=>
string(16) "Customer Service"
["subject"]=>
string(4) "2323"
["body"]=>
string(5) "23232"
["order"]=>
NULL
["responseid"]=>
NULL
["type"]=>
NULL
["priority"]=>
string(6) "Normal"
}

我无法弄清楚如何使用绑定的PDO值查看实际的SQL查询。有可能产品吗?无论如何我都能找出为什么不写信给桌子?

2 个答案:

答案 0 :(得分:1)

设置$stmtfalse不是$stmt->execute();false也不会返回errorInfo()

如果他们这样做,那么使用可用于PDO连接和语句的{{1}}方法进行调试。

答案 1 :(得分:1)

您实际上并没有使用返回值(成功消息或异常消息)执行任何操作。

要了解正在发生的事情,您可以更改:

$tkt->submitticket();

为:

echo $tkt->submitticket();

除此之外,$stmt是函数中的局部变量,因此如果要查看其内容,则需要在函数本身中var_dump