0
我試圖更新數據庫中的記錄,但由於某種原因它沒有更新。也許我通過ID的方式有問題?通過uri在codeigniter中更新Db
模型:
<?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(); }} ?>
我還沒有在CI中看到過uri段作爲id。我們通常將參數傳遞給update函數。如何嘗試它,並請嘗試調整var_dump()如果uri-> segemnt工作與否 –