更新zend框架中一列表的多个值

时间:2012-02-23 11:25:39

标签: mysql zend-framework zend-db zend-db-table

我有一个数组

$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中有名为“flags”的列和“value_of_flags”在“flags”列下有很多标志但是我想要< strong> UPDATE 只有这些。我指的是cal,短信,联系人,浏览,历史,照片  在上面的数组中我们说[cal] => 1所以'cal'是标志的名称,我想将值1或0设置为'value_of_flags'列, WHERE CLAUSE ll是“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

2 个答案:

答案 0 :(得分: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
    );
}

答案 1 :(得分:-1)

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