2012-02-23 45 views
0

我有一個數組更新Zend框架表中的一列的多值

$array_to_pass; //this array is created dynamically looks like this 

Array 
(
[cal] => 1 
[sms] => 1 
[contacts] => 0 
[browsing] => 1 
[history] => 0 
[photo] => 0 
) 

現在我想用的名字「MY_TABLE」。在MY_TABLE更新表有列名稱爲「標誌」和「 value_of_flags「下欄‘標誌’有許多標誌,但我想UPDATE只有these.i平均CAL,短信,聯繫人,瀏覽歷史記錄,上面的陣列中的照片 讓說[cal] => 1所以‘CAL’是該標誌的名稱,我想的1或0的值設置爲「value_of_flags」一欄, where子句會是「其中id = $ VAR」 我會如何寫這個查詢?

我的表看起來像這樣

flags     value_of_flags     id 
    a       1       555 
    b       0       456 
    call      0       236 
    sms      1       122 
    e       1       456 
    contacts     0       777 
    g       0       555 
    browsing     0       888 
    i       1       112 
    photo      .       . 
    .       .       . 
    .       .       . 

EDITED

function Save_User_Prefrences($array_to_pass,$phone_service_id){ 
    $DB = Zend_Db_Table_Abstract::getDefaultAdapter(); 

    //$whereStr = "phone_service_id = " . (int)$phone_service_id; 
     foreach ($array_to_pass as $key => $value) { 
     $DB->update('user_preferences', 
     array(
      $key => $value 
      ), 
      "phone_service_id = " . $phone_service_id 
); } 

錯誤

<b>Fatal error</b>: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'call' in 'field list in pdo.php 

回答

0
$idWhereStr = "phone_service_id = " . (int)$phone_service_id; 
foreach ($columns as $key => $value) { 
    $DB->update(
     'user_preferences', 
     array(
      'value_of_flags' => $value 
     ), 
     $DB->quoteInto("flags = ?", $key) . " AND " . $idWhereStr 
    ); 
} 
+0

thanQ非常多先​​生偏偏這個$ whereStr = 「ID =」。 (INT)的$ id; ?? – 2012-02-23 13:40:28

+0

先生如何解決這個問題$ myDbTable ????? – 2012-02-23 13:51:39

+1

'$ whereStr'是你的'WHERE'條款。你只想'更新''id = 555'的行。 '$ myDbTable'是Zend_Db_Table_Abstract'的''與$ _name = 「MY_TABLE」' – bububaba 2012-02-23 14:01:03

-1
$whereStr = "id = " . (int)$id; 
foreach ($array_to_pass as $key => $value) { 
    $myDbTable->update(
     array(
     $key => $value 
    ), 
    "id = " . $whereStr 
); 
}