我正在学习codeigniter,我有一个页面可以更新我的数据库。但是我得到了一个这样的错误:
类型:ArgumentCountError
消息:函数Inhouse::ubah((的参数太少,在第532行的D:\examplep\htdocs\slc\system\core\CodeIgniter.php中传递了0,并且正好需要1个
文件名:D:\examplep\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>
获取这样的参数"$this->uri->segment(2(">
exp:-htts://localhost/ci/50/60
$this->uri->segment(2(//O/P50
$this->uri->segment(3(//O/p 60
有关更多信息,请访问此处分段
public function ubah(){
$this->uri->segment(2);
}
我想,只有当你试图访问没有之类参数的controler时,才会发生这个错误
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();
}
}
谢谢你们的回答,我真的很感谢你们帮我修复代码。。但是,我只是从form_opener_multipart((中删除了"action";
由此:
<?= form_open_multipart('inhouse/ubah'); ?>
进入:
<?= form_open_multipart(); ?>
我真的不明白为什么会这样,但现在我的程序可以运行了,你们能向我解释一下吗?我真的很感激。
还有一个问题,我的数据库表中除了"图像"列之外的其他列都可以更新。