0

再次陷入对象/类。我有一个名为 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 查询。有可能生产吗?有什么办法可以找出为什么它不写到桌子上?

4

2 回答 2

1

您实际上并没有对返回值(成功消息或异常消息)做任何事情。

要查看发生了什么,您可以更改:

$tkt->submitticket();

至:

echo $tkt->submitticket();

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

于 2013-01-16T14:41:22.727 回答
1

检查那$stmt不是false在设置时,$stmt->execute();也不返回false

如果他们这样做,则使用errorInfo()可用于 PDO 连接和语句的方法进行调试。

于 2013-01-16T14:39:45.353 回答