【问题标题】:ArgumentCountError - Too few argumentsArgumentCountError - 参数太少
【发布时间】:2020-08-04 13:25:26
【问题描述】:

我正在学习 codeigniter,我有一个页面可以更新我的数据库。但我收到这样的错误:

类型:ArgumentCountError

消息:函数 Inhouse::ubah() 的参数太少,在第 532 行的 D:\xampp\htdocs\slc\system\core\CodeIgniter.php 中传递了 0,而预期正好是 1

文件名:D:\xampp\htdocs\slc\application\controllers\Inhouse.php

行号:120

我该如何解决这个问题?这是我的代码:


控制器:

public function ubah($id)
    {
        $data['title'] = 'Change Form - Data IHT Program';
        $data['inhouse'] = $this->Inhouse_model->getInhouseById($id);
        $data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('subtitle', 'Subtitle', 'required');
        $this->form_validation->set_rules('overview', 'Overview', 'required');
        $this->form_validation->set_rules('goals', 'Goals', 'required');
        $this->form_validation->set_rules('agenda1', 'Agenda 01', 'required');
        $this->form_validation->set_rules('agenda2', 'Agenda 02', 'required');
        $this->form_validation->set_rules('agenda3', 'Agenda 03', 'required');
        $this->form_validation->set_rules('agenda4', 'Agenda 04', 'required');
        $this->form_validation->set_rules('agenda5', 'Agenda 05', 'required');
        $this->form_validation->set_rules('agenda6', 'Agenda 06', 'required');
        $this->form_validation->set_rules('agenda7', 'Agenda 07', 'required');
        $this->form_validation->set_rules('agenda8', 'Agenda 08', 'required');
        $this->form_validation->set_rules('trainer', 'Trainer', 'required');

        if ($this->form_validation->run() == FALSE) {
            if (!$this->session->userdata('email')) {
                $this->load->view('templates/header', $data);
            } else {
                $this->load->view('templates/login_header', $data);
            }
            $this->load->view('inhouse/ubah', $data);
            $this->load->view('templates/footer');
        } else {
            // cek jika ada gampar yg diupload
            $upload_image = $_FILES['image']['name'];

            if ($upload_image) {
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '5048';
                $config['upload_path'] = './assets/img/inhouse/';

                $this->load->library('upload', $config);

                if ($this->upload->do_upload('image')) {
                    $old_image = $data['inhouse']['image'];
                    if ($old_image != 'default.jpg') {
                        unlink(FCPATH . 'assets/img/inhouse/' . $old_image);
                    } else {
                        $new_image = $this->upload->data('file_name');
                        $this->db->set('image', $new_image);
                    }
                } else {
                    echo $this->upload->display_errors();
                }
            }

            $this->db->set('image', $new_image);
            $this->db->where('id', $id);
            $this->db->update('inhouse', $data);

            $this->Inhouse_model->ubahInhouseProgram();
            $this->session->set_flashdata('flash', 'Diubah!');
            redirect('admin/product');
        }
    }

型号:

public function ubahInhouseProgram()
    {
        $data = [
            "image" => $this->input->post('image', true),
            "title" => $this->input->post('title', true),
            "subtitle" => $this->input->post('subtitle', true),
            "overview" => $this->input->post('overview', true),
            "goals" => $this->input->post('goals', true),
            "agenda1" => $this->input->post('agenda1', true),
            "agenda2" => $this->input->post('agenda2', true),
            "agenda3" => $this->input->post('agenda3', true),
            "agenda4" => $this->input->post('agenda4', true),
            "agenda5" => $this->input->post('agenda5', true),
            "agenda6" => $this->input->post('agenda6', true),
            "agenda7" => $this->input->post('agenda7', true),
            "agenda8" => $this->input->post('agenda8', true),
            "trainer" => $this->input->post('trainer', true)
        ];
        $this->db->where('id', $this->input->post('id'));
        $this->db->update('inhouse', $data);
    }

查看:

<div class="container">

    <div class="row mt-5 mb-5">
        <div class="col-lg-12">

            <!-- FORM -->
            <div class="card myshadow">
                <div class="card-header font-weight-bold">
                    <h2>Change Form Data IHT Program</h2>
                </div>
                <div class="card-body">


                    <?= form_open_multipart('inhouse/ubah'); ?>

                    <input type="hidden" name="id" value="<?= $inhouse['id']; ?>">


                    <div class="form-group mt-4">
                        <div class="">Picture</div>
                        <div class="row">
                            <div class="col-sm-4">
                                <img src="<?= base_url('assets/img/inhouse/') . $inhouse['image']; ?>" class="img-thumbnail">
                            </div>
                            <div class="col-sm-8">
                                <div class="custom-file">
                                    <input type="file" class="custom-file-input" id="image" name="image">
                                    <label class="custom-file-label" for="image">Choose file</label>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-group mt-4">
                        <label for="title">Title</label>
                        <input type="text" name="title" class="form-control" id="title" value="<?= $inhouse['title'] ?>">
                        <small class="form-text text-danger"><?= form_error('title') ?></small>
                    </div>

                    <div class="form-group mt-4">
                        <label for="subtitle">Subtitle</label>
                        <textarea type="text" name="subtitle" class="form-control" id="subtitle" rows="3"><?= $inhouse['subtitle']; ?></textarea>
                        <small class="form-text text-danger"><?= form_error('subtitle') ?></small>
                    </div>

                    <div class="form-group mt-4">
                        <label for="overview">Overview</label>
                        <textarea type="text" name="overview" class="form-control" id="overview" rows="8"><?= $inhouse['overview'] ?></textarea>
                        <small class="form-text text-danger"><?= form_error('overview') ?></small>
                    </div>

                    <div class="form-group mt-4">
                        <label for="goals">Goals</label>
                        <textarea type="text" name="goals" class="form-control" id="goals" rows="3"><?= $inhouse['goals'] ?></textarea>
                        <small class="form-text text-danger"><?= form_error('goals') ?></small>
                    </div>

                    <hr class="mt-5">

                    <div class="form-group mt-4">
                        <label for="agenda1">Agenda 01</label>
                        <input type="text" name="agenda1" class="form-control" id="agenda1" value="<?= $inhouse['agenda1'] ?>">
                        <small class="form-text text-danger"><?= form_error('agenda1') ?></small>
                    </div>

                    <div class="form-group mt-4">
                        <label for="agenda2">Agenda 02</label>
                        <input type="text" name="agenda2" class="form-control" id="agenda2" value="<?= $inhouse['agenda2'] ?>">
                        <small class="form-text text-danger"><?= form_error('agenda2') ?></small>
                    </div>

                    <div class="form-group mt-4">
                        <label for="agenda3">Agenda 03</label>
                        <input type="text" name="agenda3" class="form-control" id="agenda3" value="<?= $inhouse['agenda3'] ?>">
                        <small class="form-text text-danger"><?= form_error('agenda3') ?></small>
                    </div>

                    <div class="form-group mt-4">
                        <label for="agenda4">Agenda 04</label>
                        <input type="text" name="agenda4" class="form-control" id="agenda4" value="<?= $inhouse['agenda4'] ?>">
                        <small class="form-text text-danger"><?= form_error('agenda4') ?></small>
                    </div>

                    <div class="form-group mt-4">
                        <label for="agenda5">Agenda 05</label>
                        <input type="text" name="agenda5" class="form-control" id="agenda5" value="<?= $inhouse['agenda5'] ?>">
                        <small class="form-text text-danger"><?= form_error('agenda5') ?></small>
                    </div>

                    <div class="form-group mt-4">
                        <label for="agenda6">Agenda 06</label>
                        <input type="text" name="agenda6" class="form-control" id="agenda6" value="<?= $inhouse['agenda6'] ?>">
                        <small class="form-text text-danger"><?= form_error('agenda6') ?></small>
                    </div>

                    <div class="form-group mt-4">
                        <label for="agenda7">Agenda 07</label>
                        <input type="text" name="agenda7" class="form-control" id="agenda7" value="<?= $inhouse['agenda7'] ?>">
                        <small class="form-text text-danger"><?= form_error('agenda7') ?></small>
                    </div>

                    <div class="form-group mt-4">
                        <label for="agenda8">Agenda 08</label>
                        <input type="text" name="agenda8" class="form-control" id="agenda8" value="<?= $inhouse['agenda8'] ?>">
                        <small class="form-text text-danger"><?= form_error('agenda8') ?></small>
                    </div>

                    <div class="form-group mt-4">
                        <label for="trainer">Trainer</label>
                        <input type="text" name="trainer" class="form-control" id="trainer" value="<?= $inhouse['trainer'] ?>">
                        <small class="form-text text-danger"><?= form_error('trainer') ?></small>
                    </div>
                    <button type="submit" name="ubah" class="btn btn-primary mt-5">Change Data</button>
                    </form>
                </div>
            </div>
            <!-- END FORM -->

        </div>
    </div>

</div>

【问题讨论】:

  • 你的路由定义是什么样的?

标签: php codeigniter error-handling


【解决方案1】:

得到这样的参数“$this->uri->segment(2)”

exp:- htts://localhost/ci/50/60

$this->uri->segment(2) // O/P 50

$this->uri->segment(3) // O/P 60

更多信息请点击这里Segments

public function ubah(){
 $this->uri->segment(2);
}

【讨论】:

  • 不好意思,看了Codeigniter的文档还是不太明白,我的代码还是不能运行……不介意的话,能不能详细解释一下?非常感谢
【解决方案2】:

我猜,这个错误只有在你尝试访问控制器时才发生,而没有像

这样的参数
http://localhost/slc/index.php/ubah

如果你像下面这样访问控制器,应该不会出错

http://localhost/slc/index.php/ubah/2

您可以通过将默认值设置为 $id 来解决此问题

public function ubah($id = -1){
    if($id != -1){
         //put your code here
    }else{
        show_404();
    }
}

【讨论】:

    【解决方案3】:

    感谢您的所有回答,非常感谢你们帮助我修复我的代码.. 但是,我只是从我的 form_opener_multipart() 中删除“动作”;

    从此:

    <?= form_open_multipart('inhouse/ubah'); ?>
    

    进入这个:

    <?= form_open_multipart(); ?>
    

    我真的不明白为什么会这样,但是现在我的程序可以运行了,你们能给我解释一下吗?我会很感激的。

    还有一个问题,我的数据库表中除了“图像”列之外的其他列都可以更新。

    【讨论】:

      猜你喜欢
      • 2018-02-08
      • 1970-01-01
      • 2019-01-08
      • 2019-06-01
      • 1970-01-01
      • 2022-08-17
      • 2020-07-30
      • 2018-10-11
      • 1970-01-01
      相关资源
      最近更新 更多