在 Codeigniter 中编辑和更新
Edit & Update In Codeigniter
更新时出现以下错误
Severity: Notice Message: Undefined variable: user
这是我的控制器:
public function update_user_id($user_id) {
if($this->input->post('submit')){
$courses = array(
'user_name'=>$this->input->post('user_name'),
'email'=>$this->input->post('email')
);
$this->users_model->update_user($user_id,$users);
$base_url=base_url();
redirect("$base_url"."Dashboard/update_user_id/$user_id");
}
$result['user']=$this->users_model->user_id($user_id);
$this->load->view('edit_user',$result);
}
这是我的看法
<?php echo form_open(base_url().'Admin/update_user_id/'.$user[0]->user_id);?>
User Name: <input type="text" name="user_name" value=" <?php echo $user[0]->user_name; ?>">
Email: <input type="text" name="email" value=" <?php echo $user[0]->user_name; ?>">
<?php echo form_close();?>
不知道代码有什么问题
始终遵循 documentation. By CI council convention, your class names should follow file names. I suppose you have that right. But you didn't follow demand for ucfirst() 文件和 class 名称。
因此,在您的案例中,文件不应命名为 CoursesModel,class 也不应命名为 CoursesModel,但您应该将文件命名为 class 课程模型。记住 ucfirst()
规则(关于 CI3+)来命名所有 classes 控制器、模型或库。
此外,如果您加载这些文件(模型和库),库始终使用 strtolower() 名称,而对于模型,您可以同时使用 strtolower()
和 ucfirst()
格式的名称。
就个人而言,我使用 strtolower 加载库,同时使用 ucfirst 名称加载模型,这样我就可以区分那些只是快速查看代码的模型。
试试:
Courses_m.php(这样我可以加快解析速度)
<?php defined('BASEPATH') or exit('Not your cup of tea.');
class Courses_m extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function update_course($course_id, $courses)
{
// your DB task --should return something
return true ?: false;
}
}
在控制器中:
Courses_c.php(在APPPATH . 'config/routes.php'
中你可以为你的路线设置你喜欢的名字)
<?php defined('BASEPATH') or exit('Not your cup of tea.');
class Courses_c extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('form_validation');//just an library loading example
$this->load->model('Courses_m');//notice first capital here
}
public function update_course_id($course_id)
{
if($this->input->post('submit'))
{
$courses = array(
'course_name'=>$this->input->post('course_name'),
'no_of_hours'=>$this->input->post('no_of_hours')
);
// pay attention on capital first when you calling a model method
// need to be the same as in constructor where file is loaded
$this->Courses_m->update_course($course_id,$courses);
// you can use this way
redirect(base_url("Dashboard/update_course_id/$course_id"));
}
// use else block to avoid unwanted behavior
else
{
$result['course']=$this->Courses_m->course_id($course_id);
$this->load->view('edit_course',$result);
}
}
}
更新时出现以下错误
Severity: Notice Message: Undefined variable: user
这是我的控制器:
public function update_user_id($user_id) {
if($this->input->post('submit')){
$courses = array(
'user_name'=>$this->input->post('user_name'),
'email'=>$this->input->post('email')
);
$this->users_model->update_user($user_id,$users);
$base_url=base_url();
redirect("$base_url"."Dashboard/update_user_id/$user_id");
}
$result['user']=$this->users_model->user_id($user_id);
$this->load->view('edit_user',$result);
}
这是我的看法
<?php echo form_open(base_url().'Admin/update_user_id/'.$user[0]->user_id);?>
User Name: <input type="text" name="user_name" value=" <?php echo $user[0]->user_name; ?>">
Email: <input type="text" name="email" value=" <?php echo $user[0]->user_name; ?>">
<?php echo form_close();?>
不知道代码有什么问题
始终遵循 documentation. By CI council convention, your class names should follow file names. I suppose you have that right. But you didn't follow demand for ucfirst() 文件和 class 名称。
因此,在您的案例中,文件不应命名为 CoursesModel,class 也不应命名为 CoursesModel,但您应该将文件命名为 class 课程模型。记住 ucfirst()
规则(关于 CI3+)来命名所有 classes 控制器、模型或库。
此外,如果您加载这些文件(模型和库),库始终使用 strtolower() 名称,而对于模型,您可以同时使用 strtolower()
和 ucfirst()
格式的名称。
就个人而言,我使用 strtolower 加载库,同时使用 ucfirst 名称加载模型,这样我就可以区分那些只是快速查看代码的模型。
试试:
Courses_m.php(这样我可以加快解析速度)
<?php defined('BASEPATH') or exit('Not your cup of tea.');
class Courses_m extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function update_course($course_id, $courses)
{
// your DB task --should return something
return true ?: false;
}
}
在控制器中:
Courses_c.php(在APPPATH . 'config/routes.php'
中你可以为你的路线设置你喜欢的名字)
<?php defined('BASEPATH') or exit('Not your cup of tea.');
class Courses_c extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('form_validation');//just an library loading example
$this->load->model('Courses_m');//notice first capital here
}
public function update_course_id($course_id)
{
if($this->input->post('submit'))
{
$courses = array(
'course_name'=>$this->input->post('course_name'),
'no_of_hours'=>$this->input->post('no_of_hours')
);
// pay attention on capital first when you calling a model method
// need to be the same as in constructor where file is loaded
$this->Courses_m->update_course($course_id,$courses);
// you can use this way
redirect(base_url("Dashboard/update_course_id/$course_id"));
}
// use else block to avoid unwanted behavior
else
{
$result['course']=$this->Courses_m->course_id($course_id);
$this->load->view('edit_course',$result);
}
}
}