2014-06-17 89 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