我正在尝试更新数据库中的记录,但由于某种原因它没有更新。也许我传递ID的方式有问题?
模特:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class site_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_records()
{
$query = $this->db->get('posts');
return $query->result();
}
function get_records_by_id()
{
$this->db->where('postID', $this->uri->segment(3));
$query = $this->db->get('posts');
return $query->result();
}
function add_records($data) {
$this->db->insert('posts', $data);
return;
}
function updata_records($data) {
$this->db->where('postID', $this->uri->segment(3));
$this->db->update('posts', $data);
}
function delede_roe() {
$this->db->where('postID', $this->uri->segment(3));
$this->db->delete('posts');
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
controoler:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class site extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('home');
}
function add_post() {
$this->load->view('add_post');
}
function creata() {
$data = array (
'title' => $this->input->post('title'),
'post' => $this->input->post('content')
);
$this->load->model('site_model');
$this->site_model->add_records($data);
redirect('site/all_posts/');
}
function edit_post () {
if ($query = $this->site_model->get_records_by_id()) {
$data['record'] = $query;
}
$this->load->view('edit_post', $data);
}
function updata() {
$data = array (
'title' => $this->input->post('title'),
'post' => $this->input->post('content')
);
$this->load->model('site_model');
$this->site_model->updata_records($data);
redirect('site/all_posts/');
}
function all_posts() {
if ($query = $this->site_model->get_records()) {
$data['records'] = $query;
}
$this->load->view('all_posts', $data);
}
function delete() {
$this->site_model->delede_roe();
redirect('site/all_posts/');
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
form:
<?php if (isset($record)) {
foreach ($record as $row) {
echo form_open('index.php/site/updata'); ?>
<label for="title"> title </label>
<input type="text" name="title" id="title" value="<?php echo $row->title; ?>">
<label for="content"> content </label>
<input type="text" name="content" value="<?php echo $row->post; ?>" id="content" style="width: 630px;">
<input type="submit" value="׳©׳�׳—">
<?php echo form_close(); }} ?>
答案 0 :(得分:0)
updata_records($ data),我认为问题在于,模型函数应该有$ data数组的uri数据。
$data = array (
'postID' => $this->uri->segment(3),
'post_content' => $post_content
);
$this->site_model->updata_records($data);