php - 如何在 codeigniter 中更新表而不重复

标签 php mysql codeigniter checkbox

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

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

插入从注释为//inserting 1 by 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
            ?>

关于php - 如何在 codeigniter 中更新表而不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28232131/

相关文章:

php - Mysql PHP插入带引号和双引号

mysql - 映射关系——外键或映射表

php - 使用 select 形式更新 Mysql 中的数据

jquery - 使用 jQuery 和 CodeIgniter 执行客户端和服务器端验证

php - 如何使用 Codeigniter 编辑 MySQL 表行?

php - 在oracledb中设置默认数据库

php - 从sql中输出php数据

php - 如何根据 MySQL 和 PHP 中显示的行更新值

php - 如何使用 jQuery 通过 Ajax 发送复选框数组的值?

mysql - C、如何将字符串保存到二进制文件中?