如何在代码点火器中更新没有重复项的表


How to update table without duplicates in codeigniter

我正在尝试使用文本框更新复选框值,我已经完成了更新及其工作,但是当我启用新复选框并将一些值放入文本框时,值将随机时间插入mysql db。这是我观点的一部分:

   <fieldset>
                    <!--        Form Name -->                
                    <legend>Изберете типове за имота</legend>
<!--                 Multiple Checkboxes (inline) -->
                <div class="form-group">
                  <div class="col-md-4">
                    <label class="checkbox-inline" for="checkboxes-0">
                    <?php echo form_checkbox('data[1][property_type_id]', '22', set_value('data[1][property_type_id]',$type[22]['property_type_id']), 'id="checkboxes-0"')?>
                      Пентхаус
                    </label>
                      <br>
            <!--    Text input-->
                <div class="form-group">
                  <div class="col-md-8">
                    <?php echo form_input('data[1][alt_txt]', set_value('data[1][alt_txt]', isset($type[22]['alt_txt'])?$type[22]['alt_txt']:''), 'class="form-control" id="cena" placeholder="Въведи цена"')?>
                  </div>
                </div>
                    <label class="checkbox-inline" for="checkboxes-1">
                <?php echo form_checkbox('data[2][property_type_id]', '21', set_value('data[2][property_type_id]', $type[21]['property_type_id']), 'id="checkboxes-1"')?>
                      Гараж/Паркомясто
                    </label>
                <br>
            <!-- Text input-->
                <div class="form-group">
                  <div class="col-md-8">
                    <?php echo form_input('data[2][alt_txt]', set_value('data[1][alt_txt]', isset($type[21]['alt_txt'])?$type[21]['alt_txt']:''), 'class="form-control" id="cena" placeholder="Въведи цена"')?>
                  </div>
                </div>

这是我的保存功能

    public function save_dynamic($data, $id)
{
    // Delete all
    $this->db->where('property_id', $id);
    $this->db->where('value !=', 'SKIP_ON_EMPTY');
    $this->db->delete('property_value'); 
    // Insert all
    $insert_batch = array();
// Process the POST DATA from the FORM  
    foreach($data as $key=>$value)
    {
// Do this if the key is called option
        if(substr($key, 0, 6) == 'option')
        {
            $pos = strpos($key, '_');
            $option_id = substr($key, 6, $pos-6);
            $language_id = substr($key, $pos+1);
            $val_numeric = NULL;
            if( is_numeric($value) )
            {
                $val_numeric = intval($value);
            }
            $insert_arr = array('language_id' => $language_id,
                                'property_id' => $id,
                                'option_id' => $option_id,
                                'value' => $value,
                                'value_num' => $val_numeric);
            if($value != 'SKIP_ON_EMPTY')
                $insert_batch[] = $insert_arr;
            /*
            $this->db->set(array('language_id'=>$language_id,
                                 'property_id'=>$id,
                                 'option_id'=>$option_id,
                                 'value'=>$value));
            $this->db->insert('property_value');
            */
        }
    }
    // getting info from checkboxes
    $data1=$this->input->post('data'); 
    if(count($insert_batch) > 0) {
        $this->db->insert_batch('property_value', $insert_batch); 
    }
    // We need to loop through each entry and add in the property_id    
    // inserting 1 by 1
    $insert_array = array();
    $property_id = array('property_id'=>$id);
    foreach($data1 as $array){
        if(array_filter($array)){
        $insert_array[] = array_merge($array,$property_id);
        }
    }
    if($this->db->affected_rows() > 0) {
        if(!empty($insert_array)){
            foreach ($insert_array as $key=>$arrayche){
         $this->db->where('property_type_id',$arrayche['property_type_id']);
         $this->db->update('property_type_details', $arrayche);
        }
}

插入从注释到//插入 1 个 1 的地方开始,第二个问题是当我取消选中复选框时,它没有用 NULL 更新表格。.

            <form method="post" action="pagename.php">
            <input type="check" value="1" name="checkfields[]"> Field1
            <input type="check" value="1" name="checkfields[]"> Field2
            <input type="check" value="1" name="checkfields[]"> Field3
            </form>
            <?php
            $checkFields = $_POST['checkfields'];
            //Delete the previous data fields and insert selected data
            ?>