2014-06-17 91 views
-2

我无法用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()不工作是当我重复通$结构,$价值是重新表述,导致引用被改变...我首先想到& $值会有所帮助,但它看起来不是。

+0

在'$ value'被定义之前,您正在尝试使用'$ value - > ...'...! – deceze

+0

像他们这样做:[链接](http://www.php.net//manual/en/pdo.prepared-statements.php),这是官方的PHP手册。也许这不适用于对象? – Ceyfiroth

回答

1

它看起来像我问了太多与对象绑定的功能,不过,这里是一个小的解决方法:

$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()不工作是当我遍历通过$结构,$值重新表示,导致引用被改变...我首先想到& $值会有所帮助,但它看起来不是。

1

就像他们在这里做的:...

不,他们没有。再看:

$stmt->bindParam(':name', $name); 
$name = 'one'; 
$stmt->execute(); 

它们通过引用结合变量$name,然后为其赋值$name
你,另一方面是这样做的:

$stmt->bindParam(':idagent', $value->idagent); 
foreach ($structure as $key => &$value) { 
    $stmt->execute(); 
} 

您具有约束力的参考$value->idagent(我没有真正确定其是否正常工作,创造了一个隐含的对象......?!),然后你参考覆盖$value。如果有的话,您应该为绑定的$value->idagent分配一个值。只是更换$value对象根本不是一回事。 PHP不是那么聪明,它跟踪你绑定该对象的idagent属性,并且在切换底层对象之后将重建该属性。这太过元。

我认为在这种情况下,在foreach循环中使用bindValue是最有用的,并且绑定那里的现有值。

$stmt = $db->prepare($query); 
foreach ($structure as $key => $value) { 
    $stmt->bindValue(':field', $key); 
    $stmt->bindValue(':idagent', $value->idagent); 
    $stmt->bindValue(':idcontext', $value->idcontext); 
    $stmt->bindValue(':idaction', $value->idaction); 
    $stmt->bindValue(':comment', $value->comment); 
    $stmt->execute() 
} 
+0

是的,我问的绑定对象值太多...但它不工作你的方式,我之前尝试过,每次得到这个错误:“SQLSTATE [22P02]:无效的文本表示”。尽管如此,我还是采取了一些解决方法。 – Ceyfiroth

+0

而且我不确定每次迭代(记录它的方式)时都要重新定义bindValue/bindParam。 – Ceyfiroth

+0

海事组织这是最直接的方法。除语法外,它几乎没有任何区别。 – deceze