0

当我尝试在 php-codeigniter 中更新查询时,我收到了这个 form_validation 错误。这是我的控制器,这给了我错误,我无法更新或在我的数据库中添加条目

 <?php

class Person extends CI_Controller {

// num of records per page
private $limit = 15;
function Person() {
parent::__construct();


//$this->load->library('table','form_validation');
$this->load->library('table');
$this->load->library('form_validation');
$this->form_validation->set_rules();
$this->load->helper('url');

$this->load->model('personModel', '', TRUE);
}

function index($offset = 0) {

$uri_segment = 3;
$offset = $this->uri->segment($uri_segment);

// load data
$persons = $this->personModel->get_paged_list($this->limit, $offset)->result();
// have pagntn
$this->load->library('pagination');
$config['base_url'] = site_url('person/index/');
$config['total_rows'] = $this->personModel->count_all();
$config['per_page'] = $this->limit;
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();

// generate table data
$this->load->library('table');
$this->table->set_empty("&nbsp;");
$this->table->set_heading('No', 'Name', 'Gender', 'Education', 
'Date of Birth (dd-mm-   yyyy)', 'Interest', 'Actions');
$i = 0 + $offset;
foreach ($persons as $person) {
$this->table->add_row(++$i, $person->name, strtoupper($person->gender) == 
'M' ? 'Male'  : 'Female', ($person->education),
date('d-m-Y',     strtotime($person->dob)), ($person->interest), 
anchor('person/view/' . $person->id, 'view', array('class' => 'view',
'onclick' =>   "return confirm('View Full Details?')")) . ' ' .
anchor('person/update/' . $person->id, 'update', array('class' => 'update')) . ' ' .
anchor('person/delete/' . $person->id, 'delete', array('class' => 
'delete', 'onclick' => "return confirm('Are you sure want to delete this person?')"))
);
}
$data['table'] = $this->table->generate();
$this->load->library('table');
$this->load->library('form_validation');

// load view
$this->load->view('personList', $data);
}

function add() {

// set validation propert/ies
$this->set_fields();
// set common properties
$data['title'] = 'Add new person';
$data['message'] = '';
$data['action'] = site_url('person/addPerson');
$data['link_back'] = anchor('person/index/', 'Back to list of persons',
array('class' => 'back'));

// load view
$this->load->view('personEdit', $data);
}

function addPerson() {
// set common properties
$data['title'] = 'Add new person';
$data['action'] = site_url('person/addPerson');
$data['link_back'] = anchor('person/index/', 'Back to list of persons', 
array('class' => 'back'));

// set validation properties
$this->set_fields();
$this->set_rules();

// run validation
if ($this->form_validation->run() == FALSE) {
$data['message'] = '';
} else {
// save data
$person = array('name' => $this->input->post('name'),
'gender' => $this->input->post('gender'),
'dob' => date('Y-m-d', strtotime($this->input->post('dob'))));
$id = $this->personModel->save($person);
// set form input name="id"
$this->form_validation->id = $id;

// set user message
$data['message'] = '<div class="success">add new person success</div>';
}

// load view
$this->load->view('personEdit', $data);
}



function view($id) {
// set common properties
$data['title'] = 'Person Details';
$data['link_back'] = anchor('person/index/', 'Back to list of persons', 
array('class' => 'back'));

// get person details
$data['person'] = $this->personModel->get_by_id($id)->row();

// load view
$this->load->view('personView', $data);
}

function update($id) {
// set validation properties
$this->set_fields();

// prefill form values
$person = $this->personModel->get_by_id($id)->row();
$this->form_validation->id = $id;
$this->form_validation->name = $person->name;
$_POST['gender'] = strtoupper($person->gender);
$this->form_validation->dob = date('d-m-Y', strtotime($person->dob));

// set common properties
$data['title'] = 'Update person';
$data['message'] = '';
$data['action'] = site_url('person/updatePerson');
$data['link_back'] = anchor('person/index/', 'Back to list of persons', 
array('class' => 'back'));

// load view
$this->load->view('personEdit', $data);
}

function updatePerson() {
// set common properties
$data['title'] = 'Update person';
$data['action'] = site_url('person/updatePerson');
$data['link_back'] = anchor('person/index/', 'Back to list of persons', 
array('class' => 'back'));

// set validation properties
$this->set_fields();
$this->set_rules();

// run validation
if ($this->form_validation->run() == FALSE) {
$data['message'] = '';
} else {
// save data
$id = $this->input->post('id');
$person = array('name' => $this->input->post('name'),
'gender' => $this->input->post('gender'),
'dob' => date('Y-m-d', strtotime($this->input->post('dob'))),
'education'=>$this->input->post('education'),
'interest'=>$this->input->post('interest'));
$this->personModel->update($id, $person);
// set user message
$data['message'] = '<div class="success">update person success</div>';
}

// load view
$this->load->view('personEdit', $data);
}

function delete($id) {
// delete person
$this->personModel->delete($id);

// redirect to person list page
redirect('person/index/', 'refresh');
}

// validation fields
function set_fields() {

$fields['id'] = 'id';
$fields['name'] = 'name';
$fields['gender'] = 'gender';
$fields['dob'] = 'dob';
$fields['education']='educaiton';
$fields['interest']='

$this->form_validation->set_fields('$fields');
//echo"hellofff";
//exit;
}

// validation rules
function set_rules() {
$rules['name'] = 'trim|required';
$rules['gender'] = 'trim|required';
$rules['dob'] = 'trim|required|callback_valid_date';
$rules['education']='trim|required';
$rules['interest']='trim|required';
$this->form_validation->set_rules($rules);
$this->form_validation->set_message('required', '* required');
$this->form_validation->set_message('isset', '* required');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
}

function form_validation() {
$this->add();
}

// date_validation callback
function valid_date($str) {
if (!ereg("^(0[1-9]|1[0-9]|2[0-9]|3[01])-(0[1-9]|1[012])-([0-9]{4})$", $str)) {
$this->form_validation->set_message('valid_date', 'date format is not 
valid. dd-mm-yyyy');
return false;
} else {
return true;
}
}

}

?>







 This is my model:
 PersonMOdel: Database name is employeeregistration and tableused is tbl_person
<?php
class PersonModel extends CI_Model {
// table name
private $tbl_person= 'tbl_person';

function Person(){
parent::construct();
}
// get number of persons in database
function count_all(){

return $this->db->count_all($this->tbl_person);
}
// get persons with paging
function get_paged_list($limit = 15, $offset = 0){

$this->db->order_by('id','asc');
return $this->db->get($this->tbl_person, $limit, $offset);
}
// get person by id
function get_by_id($id){
$this->db->where('id', $id);
return $this->db->get($this->tbl_person);
}
// add new person
function save($person){
$this->db->insert($this->tbl_person, $person);
return $this->db->insert_id();
}
// update person by id
function update($id, $person){
$this->db->where('id', $id);
$this->db->update($this->tbl_person, $person);
}
// delete person by id
function delete($id){
$this->db->where('id', $id);
$this->db->delete($this->tbl_person);
}
}
?>

这可以看作是一个 CRUD 应用程序。

4

0 回答 0