使用Codeigniter将复选框值传递到mysql数据库 [英] Passing checkbox values to mysql database using Codeigniter

查看:156
本文介绍了使用Codeigniter将复选框值传递到mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CodeIgniter和mySQL构建一个复选框。表单包含4个选项;每个选项只有一个复选框;用户可以选择任意组合的选项。我要执行以下操作:



1 - 对于每个复选框,使用值1(如果未选中)或2(如果选中),并将这些值传递到数据库(每个checkbox都有自己的字段)。现在,无论选中还是未选中,复选框都会向数据库发送一个值0。



2 - 一旦用户更新其复选框,我想更新数据库来反映新的值。现在,为每个复选框的更新添加一个新行。



到目前为止,我已经得到一个表单提交复选框值到数据库,一个控制器和模型):



表单

 <?php echo form_open('addFoo'); > 
< input type =checkboxname =foo1value =/>
< input type =checkboxname =foo2value =/>
< input type =checkboxname =foo3value =/>
< input type =checkboxname =foo4value =/>
<?php echo form_submit('submit','保存更改'); >
<?php echo form_close(); >

控制器

  function addFoo()
{
if($ this-> input-> post('submit')){
$ id = $ this-> input-> post('id');
$ foo1 = $ this-> input-> post('foo1');
$ foo2 = $ this-> input-> post('foo2');
$ foo3 = $ this-> input-> post('foo3');
$ foo4 = $ this-> input-> post('foo4');

$ this-> load-> model('foo_model');
$ this-> foo_model-> addFoo($ id,$ foo1,$ foo2,$ foo3,$ foo4);
}
}

模型 >

 函数addFoo($ id,$ foo1,$ foo2,$ foo3,$ foo4){
$ data = array b $ b'id'=> $ id,
'foo1'=> $ foo1,
'foo2'=> $ foo2,
'foo3'=> $ foo3 ,
'foo4'=> $ foo4
);

$ this-> db-> insert('foo_table',$ data);
}


解决方案

如果您要为所有选中的复选框插入新条目,请输入p>

  ($ this-> input-> post('foo')as $ r)
{
$ data ['fieldname'] = $ r;

$ this-> model_name-> insert($ data);
}

如果要在单个条目中的不同字段中插入所有选中的复选框值,

  foreach($ this-> input-> post('foo')as $ key => $ val )
{
$ data ['field'。$ key] = $ val;

}
$ this-> model_name-> insert($ data);


I'm using CodeIgniter and mySQL to build a checkbox form. The form contains 4 options; each option has only one checkbox; users can select any combination of the options. I want to do the following:

1 - For each checkbox, use a value of 1 (if unchecked) or 2 (if checked) and pass those values through to the database (each checkbox has its own field). Right now, whether checked or unchecked, the checkboxes are sending a value of 0 through to the database.

2 - Once users update their checkboxes, I'd like to update the database to reflect the new values. Right now, a new row is added for each update to the checkboxes.

What I've got so far is a form that submits the checkbox values to the database, a controller, and a model):

Form

<?php echo form_open('addFoo'); ?>
<input type="checkbox" name="foo1" value="" />
<input type="checkbox" name="foo2" value="" />
<input type="checkbox" name="foo3" value="" />
<input type="checkbox" name="foo4" value="" />
<?php echo form_submit('submit', 'Save Changes'); ?>
<?php echo form_close(); ?>

Controller

function addFoo()
{
    if ($this->input->post('submit')) {
        $id = $this->input->post('id');            
                $foo1 = $this->input->post('foo1');
                $foo2 = $this->input->post('foo2');
                $foo3  = $this->input->post('foo3');
                $foo4  = $this->input->post ('foo4');

    $this->load->model('foo_model');
    $this->foo_model->addFoo($id, $foo1, $foo2, $foo3, $foo4);
    }
}

Model

function addFoo($id, $foo1, $foo2, $foo3, $foo4) {
        $data = array(
            'id' => $id,
            'foo1' => $foo1,
            'foo2' => $foo2,
            'foo3' => $foo3,
            'foo4' => $foo4
        );

        $this->db->insert('foo_table', $data);
    }

解决方案

At your Controller :

if you want to insert new entry for all selected checkbox:

foreach($this->input->post('foo') as $r)
{
 $data['fieldname']=$r;

 $this->model_name->insert($data);
}

if you want to insert all selected checkbox values in different fields within single entry then,

foreach($this->input->post('foo') as $key=>$val)
    {
     $data['field'.$key]=$val;

    }
$this->model_name->insert($data);

这篇关于使用Codeigniter将复选框值传递到mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆