我無法用PDO和從JSON文件創建的對象來填充表格。你看到錯誤來自哪裏? 我使用PHP5 & PostgreSQLPHP PDO,Object,bindParam
我寫成功的代碼添加了行,但只有每行的第一列(字段)被填充,其他仍然是白色的。
我的表結構是這樣的:
CREATE TABLE ' . $infoTableName . ' (field text,type text,expefactor boolean,iduser boolean,idcontext boolean,idaction boolean,params boolean,comment text)
我的目標是這樣的:
object(stdClass)[3]
public 'timestamp' =>
object(stdClass)[4]
public 'idagent' => boolean false
public 'idcontext' => boolean false
public 'idaction' => boolean false
public 'comment' => string 'ffff' (length=4)
public 'order' =>
object(stdClass)[5]
public 'idagent' => boolean false
public 'idcontext' => boolean false
public 'idaction' => boolean false
public 'comment' => string 'none' (length=4)
public 'test' =>
object(stdClass)[6]
public 'idagent' => boolean false
public 'idcontext' => boolean true
public 'idaction' => boolean false
public 'comment' => string 'y' (length=1)
最後的PHP代碼:
$structure = json_decode($_POST['structure']);
$query = "INSERT INTO " . $infoTableName . " (field, iduser, idcontext, idaction, comment) VALUES (:field, :idagent, :idcontext, :idaction, :comment)"; //Prequery
$stmt = $db->prepare($query);
$stmt->bindParam(':field', $key);
$stmt->bindParam(':idagent', $value->idagent);
$stmt->bindParam(':idcontext', $value->idcontext);
$stmt->bindParam(':idaction', $value->idaction);
$stmt->bindParam(':comment', $value->comment);
foreach ($structure as $key => &$value) {
try {
var_dump($stmt->execute());
} catch (PDOException $e) {
var_dump($e->getMessage());
}
}
你看到錯誤? 非常感謝。
編輯:它看起來像我問了太多與對象綁定的功能,不過,這裏是一個小的解決方法:
$stmt->bindParam(':field', $key);
$stmt->bindParam(':idagent', $idagent);
$stmt->bindParam(':idcontext', $idcontext);
$stmt->bindParam(':idaction', $idaction);
$stmt->bindParam(':comment', $comment);
foreach ($structure as $key => &$value) {
$idagent = $value->idagent;
$idcontext = $value->idcontext;
$idaction = $value->idaction;
$comment = $value->comment;
try {
var_dump($stmt->execute());
} catch (PDOException $e) {
var_dump($e->getMessage());
}
}
原因bindParam()不工作是當我重複通$結構,$價值是重新表述,導致引用被改變...我首先想到& $值會有所幫助,但它看起來不是。
在'$ value'被定義之前,您正在嘗試使用'$ value - > ...'...! – deceze
像他們這樣做:[鏈接](http://www.php.net//manual/en/pdo.prepared-statements.php),這是官方的PHP手冊。也許這不適用於對象? – Ceyfiroth