如何使用选中的值更新多行并取消选中?

时间:2015-11-19 09:16:06

标签: php mysql

我有像

这样的表结构
  id | user_id | notification_id | is_email_opted | is_sms_opted
   1   88          10                 0               0
   1   88          12                 1               0

我有像

这样的复选框
<td><h6>Work Order</h6></td>
 <input type="checkbox" name="email_job" id="email_job">
         <input type="checkbox" name="work_order_email" id="work_order_email" checked disabled value="1"></label></td>     

               <input type="checkbox" name="work_order_sms" id="work_order_sms" value="1"></label></td>
                                </tr>

我必须根据user_id和notification_id将这些值更新到表中。我已经在用户注册时插入notificaiton值default。此外,当复选框已选中时,我将其存储在隐藏值中并将值发送到php

       <input type="hidden"  name="sms_job_notify" id="sms_job_notify" value="0">
              <input type="hidden" name="email_job_notify" id="email_job_notify" value="0">
            <input type="hidden" name="work_order_sms_notify" id="work_order_sms_notify" value="0">
            <input type="hidden" name="deal_email_notify" id="deal_email_notify" value="0">

我已尝试使用以下php代码来更新它是否正常工作。代码太复杂了

         if($email_job || $sms_job ){
            $update_data = array("is_email_opted"=>$email_job,"is_sms_opted"=>$sms_job);
            $this->db->where("notify_type_id",10);
            $this->db->where("user_id",$this->session->userdata('user_id'));
            $this->db->update("user_notification", $update_data);
            echo $this->db->last_query();
        }else{
            $update_data = array("is_email_opted"=>0,"is_sms_opted"=>0);
            $this->db->where("notify_type_id",10);
            $this->db->where("user_id",$this->session->userdata('user_id'));
            $this->db->update("user_notification", $update_data);
            echo $this->db->last_query();

        }

        if($work_order_sms ){
            $update_data = array("is_sms_opted"=>$work_order_sms);
            $this->db->where("notify_type_id",11);
            $this->db->where("user_id",$this->session->userdata('user_id'));
            $this->db->update("user_notification", $update_data);
            echo $this->db->last_query();
        }else{
            $update_data = array("is_sms_opted"=>0);
            $this->db->where("notify_type_id",11);
            $this->db->where("user_id",$this->session->userdata('user_id'));
            $this->db->update("user_notification", $update_data);
            echo $this->db->last_query();

        }

0 个答案:

没有答案