php - 遇到未捕获的异常类型:ArgumentCountError
问题描述
请帮助我,我无法访问我的代码,它总是显示错误:
遇到未捕获的异常
类型:参数计数错误
消息:函数 Gallery::edit() 的参数太少,在第 532 行的 C:\xampp\htdocs\eatsoc\system\core\CodeIgniter.php 中传递了 1 个,并且预期正好 2 个
文件名:C:\xampp\htdocs\eatsoc\application\controllers\backend\Gallery.php
行号:53
回溯:
文件:C:\xampp\htdocs\eatsoc\index.php
线路:315
函数:require_once
但是,在我的其他代码中,代码工作正常......
这是代码:
控制器:Gallery.php
class Gallery extends CI_Controller {
//Load Model
public function __construct()
{
parent::__construct();
$this->load->model('backend/Gallery_model');
$this->load->library('upload');
}
//Halaman Utama
public function index()
{
$gallery = $this->Gallery_model->listing();
$data = array('title' => 'Data Gallery ('.count($gallery).')',
'gallery' => $gallery,
'isi' => 'backend/gallery/list');
$this->load->view('backend/layout/wrapper', $data, FALSE);
}
//Halaman Tambah
public function tambah(){
//Validasi
$valid=$this->form_validation;
$valid->set_rules('tittle', 'tittle', 'required',
array('required' => 'tittle harus diisi'));
$valid->set_rules('file', 'file', 'required',
array('required' => 'Gambar harus diisi'));
if($valid->run()===FALSE){
$data = array('title' => 'Tambah Gallery',
'isi' => 'backend/gallery/tambah');
$this->load->view('backend/layout/wrapper', $data, FALSE);
}else{
$i= $this->input;
$data = array('id_gallery' => $i->post('id_gallery'),
'tittle' => $i->post('tittle'),
'deskripsi'=> $i->post('deskripsi'),
'file'=> $i->post('file'),
);
$this->Gallery_model->tambah($data);
$this->session->set_flashdata('sukses','Data telah ditambah');
redirect(base_url('backend/gallery'),'refresh');
}
//End masuk database
}
//Halaman Edit
public function edit($id_gallery){
$gallery = $this->Gallery_model->detail($id_gallery);
//Validasi
$valid=$this->form_validation;
$valid->set_rules('tittle', 'tittle', 'required',
array('required' => 'tittle harus diisi'));
if($valid->run()===FALSE){
$data = array('title' => 'Edit Gallery: '.$gallery->tittle,
'gallery' => $gallery,
'isi' => 'backend/gallery/edit');
$this->load->view('backend/layout/wrapper', $data, FALSE);
} else{
$i= $this->input;
$this->Gallery_model->edit($data);
$this->session->set_flashdata('sukses','Data telah diubah');
redirect(base_url('backend/gallery'),'refresh');
}
//End masuk database
}
//Halaman Delete
public function delete($id_gallery){
$data = array('id_gallery' => $id_gallery);
$this->Gallery_model->delete($data);
$this->session->set_flashdata('sukses', 'Data telah dihapus');
redirect(base_url('backend/gallery'),'refresh');
}
}
型号:Gallery_model.php
class Gallery_model extends CI_Model{
public function __construct()
{
parent::__construct();
$this->load->database();
}
//Listing
public function listing(){
$this->db->select('*');
$this->db->from('gallery');
$this->db->order_by('id_gallery');
$query = $this->db->get();
return $query->result();
}
//Detail
public function detail($id_gallery){
$this->db->select('*');
$this->db->from('gallery');
$this->db->where('id_gallery',$id_gallery);
$this->db->order_by('id_gallery');
$query = $this->db->get();
return $query->row();
}
// Tambah
public function tambah($data){
$this->db->insert('gallery', $data);
}
// Edit
public function edit($id_gallery, $data){
$this->db->where('id_gallery', $data['id_gallery']);
$this->db->update('gallery', $data['id_gallery']);
}
// delete
public function delete($data){
$this->db->where('id_gallery', $data['id_gallery']);
$this->db->delete('gallery', $data);
}
}
在视图中:文件夹 Admin/Gallery edit.php
<!-- Content Header (Page header) -->
<section class="content-header">
<h1> Gallery </h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"> Dasbor</i></a></li>
<li><a href="<?php echo base_url('backend/gallery')?>"><i class="fa fa-table"> </i>Gallery</a></li>
<li class="active"> Edit</li>
</ol>
</section>
<div class="box">
<div class="box-header">
<p>
<a href="<?php echo base_url('backend/gallery/edit')?>" class="btn btn-success">
<i class="fa fa-plus"></i>Edit</a>
</p>
<?php
//Notifikasi
if($this->session->flashdata('sukses')){
echo '<div class="alert alert-success"><i class="fa fa-check">';
echo $this->session->flashdata('sukses');
echo '</div>';
}?>
</div>
<div class="box-body">
<?php
//Notifikasi kalau ada input error
echo validation_errors('<div class="alert alert-danger"><i class="fa fa-warning"></i>','</div>');
//Open Form
echo form_open(base_url('backend/gallery/edit'));
?>
<div class="col-md-12">
<div class="form-group">
<label>Gambar</label><input type="file" name="file" class="dropify" style="width: 100%; height: 200px;" required> <?php echo $gallery->file?>
</div>
<br>
<div class="form-group">
<label>tittle</label>
<input type="text" name="tittle" class="form-control" placeholder="tittle" value="<?php echo $gallery->tittle?>" required>
</div>
<div class="form-group">
<div class="box">
<div class="box-header">
<h3 class="box-title">Deskripsi</h3>
</div>
<textarea class="text" placeholder="Deskripsi" style="width:100%;height: 200px;line-height: 18px; border: 1px solid #dddddd; padding: 10px;" name="deskripsi"><?php echo $gallery->deskripsi?></textarea>
</div>
</div>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-success btn-lg" value="edit">
<input type="reset" name="submit" class="btn btn-default btn-lg" value="Reset">
</div>
</div>
<?php
//Form Close
echo form_close();
?>
</div>
解决方案
在Gallery.php
更改此行
public function edit($id_gallery){
...
$this->Gallery_model->edit($data);
...
}
至
public function edit($id_gallery){
...
$this->Gallery_model->edit($id_gallery, $data);
...
}
这是因为该方法(在 Gallery_model 中)被定义为:
public function edit($id_gallery, $data){ ... }
这需要画廊 ID 和数据。你只给它发送了数据...
选项 2
ID 似乎是数据的一部分的另一种选择是仅删除 Gallery_model 中的第一个参数。所以它看起来像这样:
// Edit
public function edit($data){
$this->db->where('id_gallery', $data['id_gallery']);
$this->db->update('gallery', $data['id_gallery']); //see below
}
该错误有点令人困惑,但它可能来自“画廊”控制器中调用此方法的位置。
最后的
最后一件事是这可能是错误的:
$this->db->update('gallery', $data['id_gallery']);
我不太使用 CI,但似乎如果您正在执行更新,您将需要更多数据,而不仅仅是画廊的 ID。完全有可能这应该是:
$this->db->update('gallery', $data);
但正如我所说,这些天我不怎么使用 CI,所以我不能肯定地说。但这似乎更有意义:
// Edit
public function edit($id_gallery, $data){
$this->db->where('id_gallery', $id_gallery);
$this->db->update('gallery', $data);
}
//and call it this way $this->Gallery_model->edit($id_gallery, $data);
无论如何,希望它有所帮助
干杯!
推荐阅读
- python-2.7 - Maya:寄养节点时查询约束信息
- makefile - 如何从 Bazel 导出/打包一组文件
- sql - 连接两个表,从第二个表返回所有行作为单行
- python - 客户端-服务器 python 将字节转换为字符串
- java - 不能发到后台
- javascript - AngularJS - 状态参数不会使用 ui-route 保存到控制器
- r - 在 R 中使用两个数据帧进行索引的问题
- python - 使用 boto 在 python 脚本中添加 Signature v4
- google-app-engine - 无法通过 SSH 连接到 Google Cloud
- angular - 刷新页面时 Angular 和 JWT 角色消失