用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2913|回复: 5
收起左侧

[HELP] 为什么我插入数据提示我You must use the "set" method

[复制链接]
发表于 2011-4-26 15:35:30 | 显示全部楼层 |阅读模式
#view
<form action="<?=site_url()?>/category/insert" method="post">
  <table border="0" cellpadding="2" cellspacing="1" style="width:100%">
                                          <tr>
                                            <td nowrap align="right" width="13%">上级栏目:</td>
                                            <td width="41%"><select name="pid"><option value="0">≡ 作为一级栏目 ≡</option></select></td>
                                          </tr>
                                          <tr>
                                            <td nowrap align="right" height="50px">栏目名称:</td>
                                            <td colspan="3">
                                                <input type="text" name="title" />&nbsp;&nbsp;
                                                <?php echo form_error('title','<span class="form-error">','</span>'); ?>
                                            </td>
                                            </tr>
                                          </table>
<input type="submit" value="提交" name="submit" class="inputbutton" />
            <input type="button" name="submit2" value="返回" class="inputbutton" />


#controller
function insert()
    {
        $this->lang->load('form', 'chinese');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('title', 'lang:form_title', 'trim|required');

         if ($this->form_validation->run() === false)
        {
            $this->load->view('admin/addcate');
        }
        else
        {
            $this->load->model('admin/Category_model', '', true);
            $this->Category_model->insert();
        }
    }

#model
function insert()
    {
        $this->title = $this->input->post('title');
        $this->pid = $this->input->post('pid');

        $this->db->insert('dj2_category', $this);
    }
发表于 2011-4-26 15:40:41 | 显示全部楼层
#model
function insert($arr)
    {
        $this->db->insert('dj2_category', $arr);
    }
#c
$title = $this->input->post('title');
$pid = $this->input->post('pid');
$arr = array('title'=>$title,'pid'=>$pid);
$this->Category_model->insert($arr);

改成我这样绝对不会报错...
获取表达数据应该直接写在C层

你报错是因为$this->db->insert('dj2_category', $this);
$this 数组里必须是array('字段'=>值)
发表于 2011-4-26 15:42:21 | 显示全部楼层
你传的参数不对啊
$data->title = $this->input->post('title');
        $data->pid = $this->input->post('pid');

        $this->db->insert('dj2_category', $data);
 楼主| 发表于 2011-4-26 15:50:10 | 显示全部楼层
不是你们两说的都不对,我看了一些外国人的说法,明白了,我的数据库id是自增的,我就插入pid和title没有插入id,所以才报的所,我在model里改成这样就可以了
function insert()
    {
        $this->id = 11;
        $this->title = $this->input->post('title');
        $this->pid = $this->input->post('pid');

        $this->db->insert('dj2_category', $this);
    }
但我郁闷不会每次我插入都要取数据库取最大的id吧。。。晕
 楼主| 发表于 2011-4-26 15:54:06 | 显示全部楼层
可以了啊啊啊啊
 楼主| 发表于 2011-4-26 15:55:01 | 显示全部楼层
原来是我的model里我把这几个字段设置成private就不行,不知道为什么,现在把这几个属性设成public就可以了。

本版积分规则