我无法用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