简体   繁体   中英

How to update table without duplicates in codeigniter

I'm trying to make update for checkboxes value with textboxes, I have already done update and its work but when I'm enabling new checkbox and put some value into textbox, value going inserting random times into mysql db. This is part from my view:

   <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>

and this is my save function

    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);
        }
}

inserting begins where is commented to //inserting 1 by 1 and second problem is when I uncheck checkbox its not updated with NULL the table..

            <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
            ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM