2013-02-18 92 views
0

我希望更新數據庫表中的值由codeigniter上的數組數據,但我有輸出錯誤。如何解決它?UPDATE從數組值查詢

PHP:

$mainpage = $this - > input - > post('mainpage'); 
$data2 = array(); 
foreach($mainpage as $idx = > $name) { 
    $data2[] = array(
     'mainpage' = > $name[0], 
     'subpage' = > json_encode($this - > input - > post('subpage')[$idx]), 
     'service' = > json_encode($this - > input - > post('tdadeps')[$idx]), 
     'id_relation' = > $id_relation,); 
    $this - > db - > where('id_relation', $id_relation); 
    $data22 = $this - > db - > update('access_level', $data2); 
} 

ERROR輸出:

A PHP Error was encountered 
Severity: Notice 
Message: Array to string conversion 
Filename: mysql/mysql_driver.php 
Line Number: 612 

A Database Error Occurred 
Error Number: 1054 
Unknown column '0' in 'field list' 
UPDATE `access_level` SET `0` = Array WHERE `id_relation` = '2' 
Filename: W:\system\database\DB_driver.php 
Line Number: 330 
+0

你可以發佈$主頁數組.........所以我可以爲你寫查詢,因爲錯誤說有未知列..... – 2013-02-19 05:27:19

回答

0

我希望看到更多的代碼,但試試這個代碼:

$mainpage = $this - > input - > post('mainpage'); 
$data2 = array(); 
foreach($mainpage as $idx = > $name) { 
    $data2 = array(
     'mainpage' = > $name[0], 
     'subpage' = > json_encode($this - > input - > post('subpage')[$idx]), 
     'service' = > json_encode($this - > input - > post('tdadeps')[$idx]), 
     'id_relation' = > $id_relation); 
    $this - > db - > where('id_relation', $id_relation); 
    $data22 = $this - > db - > update('access_level', $data2); 
} 

讓我知道如果工作

+0

它不起作用。插入數據庫中多行的常量值。 – 2013-02-18 21:49:20

+0

但你沒有更多的錯誤?你想從這段代碼中得到什麼? – vikingmaster 2013-02-18 21:51:42

+0

循環中的代碼('foreach ...')。插入只是最後一個循環沒有所有循環,? – 2013-02-18 22:02:32