首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在codeigniter中无重复更新表

如何在codeigniter中无重复更新表
EN

Stack Overflow用户
提问于 2015-01-30 16:43:11
回答 1查看 117关注 0票数 0

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

代码语言:javascript
复制
   <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>

这是我的save函数

代码语言:javascript
复制
    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 1 by 1的地方,第二个问题是当我取消选中复选框时,其未更新为NULL表。

EN

回答 1

Stack Overflow用户

发布于 2015-01-30 18:08:05

代码语言:javascript
复制
            <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
            ?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28232131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档