ArgumentCountError Codeigniter

时间:2019-04-03 15:15:09

标签: php codeigniter-3

我正在尝试对数据库的standardId属性进行编辑。现在,我手动在标准ID的“输入”字段中输入ID值,但理想情况下,最终将使该属性不可见。

当尝试在数据库中编辑现有属性时,出现以下错误:

类型:参数计数错误

消息:函数Standards :: edit()的参数太少,第532行的D:\ xamp \ htdocs \ myisogo \ system \ core \ CodeIgniter.php中传递了0,并且恰好是1

文件名:D:\ xamp \ htdocs \ myisogo \ application \ controllers \ Standards.php

行号:39

型号:

public function edit_standard()
{
    $this->load->helper('url');

    $data = array(
        'standardid' => $this->input->post('standardId'),
        'standardcode' => $this->input->post('standardCode'),
        'standardname' => $this->input->post('standardName')
    );

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

控制器:

    $this->load->helper('form');
    $this->load->library('form_validation');

    $data['standard'] = $this->standard_model->get_standard_byId($standard_id);

    $this->form_validation->set_rules('standardId', 'standardid', 'required');
    $this->form_validation->set_rules('standardCode', 'standardcode', 'required');
    $this->form_validation->set_rules('standardName', 'standardname', 'required');

    if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('templates/header');
        $this->load->view('templates/navbar');
        $this->load->view('standards/edit', $data);
        $this->load->view('templates/footer');
    }
    else
    {
        $this->standard_model->edit_standard();
        $this->load->view('standards/success');
    }

查看:

<?php echo form_open('standards/edit'); ?>

<h1>Amend Existing standard </h1>
<h2><?php echo $standard['standardId']; ?></h2>

<label for = "title"> Standard ID </label>
<input type="input" name = "standardId"/><br />

<label for="title">Standard Code</label>
<input type="input" name="standardCode" /><br />

<label for="text">Standard Title</label>
<textarea name="standardName"></textarea><br />

<input type="submit" name="submit" value="Create new standard" />

</form>

1 个答案:

答案 0 :(得分:1)

我假设您在Standards :: edit中的方法是:

public function edit($standard_id)
    $this->load->helper('form');
    $this->load->library('form_validation');

    $data['standard'] = $this->standard_model->get_standard_byId($standard_id);

    $this->form_validation->set_rules('standardId', 'standardid', 'required');
    $this->form_validation->set_rules('standardCode', 'standardcode', 'required');
    $this->form_validation->set_rules('standardName', 'standardname', 'required');

    if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('templates/header');
        $this->load->view('templates/navbar');
        $this->load->view('standards/edit', $data);
        $this->load->view('templates/footer');
    }
    else
    {
        $this->standard_model->edit_standard();
        $this->load->view('standards/success');
    }
}

在这种情况下,始终需要$ standard_id。对吧?

但是问题是您的form_open。您正在传递不带ID的URL。

<?php echo form_open('standards/edit'); ?>

这样,提交表单时,您将提交到//localhost/standards/edit,但是COdeIgniter仅接受//localhost/standards/edit/<some_id_here>

您有两种选择

  1. 不要将URL传递给form_open,因为您没有发布到其他端点。 <?php echo form_open(); ?>

  2. 编辑您的form_open并连接ID <?php echo form_open("standards/edit/{$standard['standardId']}"); ?>

我坚持第一个。

如果要将参数设为可选,则需要分配默认值public function edit($standard_id=null)