多个复选框更新

时间:2015-06-26 05:00:15

标签: php mysql

我想更新DB中的多个复选框。我想根据表mod_emailnotification_type中的所选类型更新电子邮件通知。我已经插入了它

$optionArray = $_POST['notification_type'];
$count=count($optionArray);
    foreach($optionArray as $option) {
    $notification_type =$option;
    $query="INSERT INTO mod_emailnotification_type(notification_type,user_id)VALUES('$notification_type',$user_id)";
    $result=mysql_query($query);
}

其中notification_type是选中的复选框。

我有以下数据库结构

  1. id(自动生成)
  2. USER_ID
  3. NOTIFICATION_TYPE
  4. 用户表与此表mod_emailnotification_type有一对多的关系。

    这是结果

    id  user_id     notification_type   
    1   14          general
    3   14          hint
    

    我正在检索并在我的tpl文件中显示

    {php}
    $query1="SELECT * from mod_emailnotification_type";
    $result1=mysql_query($query1);
     $check=array();
    while ($data=mysql_fetch_array($result1)) {
    $notification_type=$data['notification_type'];
    $notification_id=$data['id'];
    
             $check[]=$notification_type;
            $check[$notification_type]=$data['id'];
        }
    print_r($check);
    
    {/php}
    <div>
        <h1 class="email_notification">Email Notification Type</h1>
        <form action="index.php?m=networkstatus" method="post">
            {php} if(in_array("failure", $check)){
             echo '<input class="notification_type" type="checkbox" name="notification_type[]" value="failure" checked>Failure<br>'; 
             echo '<input type="hidden" name="id[]" value="'.$id.'" >'; 
    
            }
            else
             echo '<input class="notification_type" type="checkbox" name="notification_type[]" value="failure" >Failure<br>';
    
        if(in_array("hint", $check)){
         echo '<input class="notification_type" type="checkbox" name="notification_type[]" value="hint" checked>Hint<br>';  
                  echo '<input type="hidden" name="id[]" value="'.$id.'" >'; 
    
        }
        else
         echo '<input class="notification_type" type="checkbox" name="notification_type[]" value="hint" >Hint<br>';
    
         if(in_array("maintenance", $check)){
         echo '<input class="notification_type" type="checkbox" name="notification_type[]" value="maintenance" checked>Maintenance<br>';  
                  echo '<input type="hidden" name="id[]" value="'.$id.'" >'; 
    
        }
        else
         echo '<input class="notification_type" type="checkbox" name="notification_type[]" value="maintenance" >Maintenance<br>';
    
         if(in_array("system", $check)){
         echo '<input class="notification_type" type="checkbox" name="notification_type[]" value="system" checked>System<br>'; 
                  echo '<input type="hidden" name="id[]" value="'.$id.'" >'; 
    
        }
        else
         echo '<input class="notification_type" type="checkbox" name="notification_type[]" value="system" >System<br>';
    
         if(in_array("general", $check)){
         echo '<input class="notification_type" type="checkbox" name="notification_type[]" value="general" checked>General<br>'; 
                  echo '<input type="hidden" name="id[]" value="'.$id.'" >'; 
    
        }
        else
         echo '<input class="notification_type" type="checkbox" name="notification_type[]" value="general" >General<br>';
    
         if(in_array("security", $check)){
         echo '<input class="notification_type" type="checkbox" name="notification_type[]" value="security" checked>Security<br>'; 
                  echo '<input type="hidden" name="id[]" value="'.$id.'" >'; 
    
        }
        else
         echo '<input class="notification_type" type="checkbox" name="notification_type[]" value="security" >Security<br>';
    
    if($check){
             echo '<input class="btn btn-primary submit_notif" type="submit" value="Update" name="update">';   
            }
            else
             echo '<input class="btn btn-primary submit_notif" type="submit" value="Submit" name="submit">';
    
            {/php}
    
        </form>
    

0 个答案:

没有答案