CodeIgniter 添加和更新数据库没有成功,没有错误,没有,只是不会工作
CodeIgniter Add & Update Database without success, no error, nothing, just wont work
我是 PHP 和 CodeIgniter 的新手。我一直在尝试在 stud table 中添加新数据,或者编辑一些数据但没有成功。问题是我可以很好地删除数据。所以我真的不知道是什么问题。
Stud_controller
<?php
class Stud_controller extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function index(){
$query = $this->db->get('stud');
$data['records'] = $query->result();
$this->load->helper('url');
$this->load->view('Stud_view', $data);
}
public function add_student_view(){
$this->load->helper('form');
$this->load->view('Stud_add');
}
public function add_student(){
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$this->Stud_Model->insert($data);
$query = $this->db->get('stud');
$data['records'] = $query->result();
$this->load->view('Stud_view', $data);
}
public function update_student_view(){
$this->load->helper('form');
$roll_no = $this->uri->segment('3');
$query = $this->db->get_where('stud', array('roll_no' => $roll_no));
$data['records'] = $query->result();
$data['old_roll_no'] = $roll_no;
$this->load->view('Stud_edit', $data);
}
public function update_student(){
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$old_roll_no = $this->input->post('old_roll_no');
$this->Stud_Model->update($data, $old_roll_no);
$query = $this->db->get('stud');
$data['records'] = $query->result();
$this->load->view('Stud_view', $data);
}
public function delete_student(){
$this->load->model('Stud_Model');
$roll_no = $this->uri->segment('3');
$this->Stud_Model->delete($roll_no);
$query = $this->db->get('stud');
$data['records'] = $query->result();
$this->load->view('Stud_view', $data);
}
}
?>
Stud_Model
<?php
class Stud_Model extends CI_Model {
function __construct(){
parent::__construct();
}
public function insert($data){
if ($this->db->insert('stud', $data)){
return true;
}
}
public function delete($roll_no){
if ($this->db->delete('stud', 'roll_no =' . $roll_no )){
return true;
}
}
public function update($data, $old_roll_no){
$this->db->set($data);
$this->db->where('roll_no', $old_roll_no);
$this->db->update('stud', $data);
}
}
?>
Stud_add
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Students Example</title>
</head>
<body>
<form method = "" action = "">
<?php
echo form_open('Stud_controller/add_student');
echo form_label('Roll No.');
echo form_input(array('id' => 'roll_no', 'name' => 'roll_no'));
echo "<br />";
echo form_label('Name');
echo form_input(array('id' => 'name', 'name' => 'name'));
echo "<br />";
echo form_submit(array('id' => 'submit', 'value' => 'Submit'));
echo form_close();
echo "<br />";
echo "<a href='".base_url()."index.php'>Home</a></td>";
?>
</form>
</body>
</html>
Stud_edit
<!DOCTYPE html>
<html lang="eng">
<head>
<meta charset="utf-8">
<title>Students Example</title>
</head>
<body>
<form action="" method="">
<?php
echo form_open('Stud_controller/update_student');
echo form_hidden('old_roll_no', $old_roll_no);
echo form_label('Roll No.');
echo form_input(array('id'=>'roll_no', 'name'=>'roll_no', 'value'=>$records[0]->roll_no));
echo "<br />";
echo form_label('Name');
echo form_input(array('id'=>'name', 'name'=>'name', 'value'=>$records[0]->name));
echo "<br />";
echo form_submit(array('id'=>'submit', 'value'=>'Submit'));
echo form_close();
echo "<a href='".base_url()."index.php'>Home</a></td>";
?>
</body>
</html>
尝试这样的事情(控制器 update_student)
$query = $this->Stud_Model->update($data, $old_roll_no);
if ($query->result() {
$data['records'] = $query->result();
}
$this->load->view('Stud_view', $data);
您不需要 $query = $this->db->get('stud');,因为您的模型应该返回它。但是你的模型没有返回任何东西
您必须使用 if 语句启动该方法。在您的其他功能中尝试这样做,并确保您没有同时更新控制器和模型。
此外,在 类 中提及控制器和模型时,请使用小写名称。这是一个不使用模型的插入示例。确定并验证并转义您的数据
function enterPosts()
{
$this->is_logged_in();
$data = [
'title' => html_escape(trim($this->input->post('title'))),
'content' => trim($this->input->post('content')),
'date' => html_escape(trim($this->input->post('date'))),
'parent' => html_escape(trim($this->input->post('parent'))),
'status' => html_escape(trim($this->input->post('status'))),
'slug' => trim($this->input->post('slug'))
];
$this->form_validation->set_rules('title', 'Title', required|max_length[50]',));
$this->form_validation->set_rules('content', 'content', 'required|min_length[50]'));
$this->form_validation->set_rules('date', 'Date', 'required');
$this->form_validation->set_rules('parent', 'Parent', 'required');
$this->form_validation->set_rules('status', 'Status', 'required');
$this->form_validation->set_rules('slug', 'Slug', required|trim|min_length[30]'));
if( $this->form_validation->run() == FALSE) {
echo validation_errors(); //or just load your form view
}else
{
$this->db->insert('posts', $data);
}
} //end of enterposts
因为你已经说过你是新手所以
只要看看这个页面,你肯定会得到一些优惠
我是 PHP 和 CodeIgniter 的新手。我一直在尝试在 stud table 中添加新数据,或者编辑一些数据但没有成功。问题是我可以很好地删除数据。所以我真的不知道是什么问题。
Stud_controller
<?php
class Stud_controller extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function index(){
$query = $this->db->get('stud');
$data['records'] = $query->result();
$this->load->helper('url');
$this->load->view('Stud_view', $data);
}
public function add_student_view(){
$this->load->helper('form');
$this->load->view('Stud_add');
}
public function add_student(){
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$this->Stud_Model->insert($data);
$query = $this->db->get('stud');
$data['records'] = $query->result();
$this->load->view('Stud_view', $data);
}
public function update_student_view(){
$this->load->helper('form');
$roll_no = $this->uri->segment('3');
$query = $this->db->get_where('stud', array('roll_no' => $roll_no));
$data['records'] = $query->result();
$data['old_roll_no'] = $roll_no;
$this->load->view('Stud_edit', $data);
}
public function update_student(){
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$old_roll_no = $this->input->post('old_roll_no');
$this->Stud_Model->update($data, $old_roll_no);
$query = $this->db->get('stud');
$data['records'] = $query->result();
$this->load->view('Stud_view', $data);
}
public function delete_student(){
$this->load->model('Stud_Model');
$roll_no = $this->uri->segment('3');
$this->Stud_Model->delete($roll_no);
$query = $this->db->get('stud');
$data['records'] = $query->result();
$this->load->view('Stud_view', $data);
}
}
?>
Stud_Model
<?php
class Stud_Model extends CI_Model {
function __construct(){
parent::__construct();
}
public function insert($data){
if ($this->db->insert('stud', $data)){
return true;
}
}
public function delete($roll_no){
if ($this->db->delete('stud', 'roll_no =' . $roll_no )){
return true;
}
}
public function update($data, $old_roll_no){
$this->db->set($data);
$this->db->where('roll_no', $old_roll_no);
$this->db->update('stud', $data);
}
}
?>
Stud_add
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Students Example</title>
</head>
<body>
<form method = "" action = "">
<?php
echo form_open('Stud_controller/add_student');
echo form_label('Roll No.');
echo form_input(array('id' => 'roll_no', 'name' => 'roll_no'));
echo "<br />";
echo form_label('Name');
echo form_input(array('id' => 'name', 'name' => 'name'));
echo "<br />";
echo form_submit(array('id' => 'submit', 'value' => 'Submit'));
echo form_close();
echo "<br />";
echo "<a href='".base_url()."index.php'>Home</a></td>";
?>
</form>
</body>
</html>
Stud_edit
<!DOCTYPE html>
<html lang="eng">
<head>
<meta charset="utf-8">
<title>Students Example</title>
</head>
<body>
<form action="" method="">
<?php
echo form_open('Stud_controller/update_student');
echo form_hidden('old_roll_no', $old_roll_no);
echo form_label('Roll No.');
echo form_input(array('id'=>'roll_no', 'name'=>'roll_no', 'value'=>$records[0]->roll_no));
echo "<br />";
echo form_label('Name');
echo form_input(array('id'=>'name', 'name'=>'name', 'value'=>$records[0]->name));
echo "<br />";
echo form_submit(array('id'=>'submit', 'value'=>'Submit'));
echo form_close();
echo "<a href='".base_url()."index.php'>Home</a></td>";
?>
</body>
</html>
尝试这样的事情(控制器 update_student)
$query = $this->Stud_Model->update($data, $old_roll_no);
if ($query->result() {
$data['records'] = $query->result();
}
$this->load->view('Stud_view', $data);
您不需要 $query = $this->db->get('stud');,因为您的模型应该返回它。但是你的模型没有返回任何东西 您必须使用 if 语句启动该方法。在您的其他功能中尝试这样做,并确保您没有同时更新控制器和模型。 此外,在 类 中提及控制器和模型时,请使用小写名称。这是一个不使用模型的插入示例。确定并验证并转义您的数据
function enterPosts()
{
$this->is_logged_in();
$data = [
'title' => html_escape(trim($this->input->post('title'))),
'content' => trim($this->input->post('content')),
'date' => html_escape(trim($this->input->post('date'))),
'parent' => html_escape(trim($this->input->post('parent'))),
'status' => html_escape(trim($this->input->post('status'))),
'slug' => trim($this->input->post('slug'))
];
$this->form_validation->set_rules('title', 'Title', required|max_length[50]',));
$this->form_validation->set_rules('content', 'content', 'required|min_length[50]'));
$this->form_validation->set_rules('date', 'Date', 'required');
$this->form_validation->set_rules('parent', 'Parent', 'required');
$this->form_validation->set_rules('status', 'Status', 'required');
$this->form_validation->set_rules('slug', 'Slug', required|trim|min_length[30]'));
if( $this->form_validation->run() == FALSE) {
echo validation_errors(); //or just load your form view
}else
{
$this->db->insert('posts', $data);
}
} //end of enterposts
因为你已经说过你是新手所以
只要看看这个页面,你肯定会得到一些优惠