本帖最后由 80后奔三ing 于 2012-2-23 14:12 编辑
如题所示:
我在控制器里监测试图post得数据,并对一数据使用回调检测,代码如下:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller{
public function __construct() { parent::__construct(); $this->load->helper('form'); $this->load->library('form_validation'); $this->load->model('User_Model'); }
public function index() { $this->load->view('admin/user_add'); }
public function add_user() { if($this->_form_validation() === FALSE) { $this->index(); } else { $data['username'] = $this->input->post('username',TRUE); $data['realname'] = $this->input->post('realname',TRUE); $data['sex'] = $this->input->post('sex',TRUE); $data['email1'] = $this->input->post('email1',TRUE); $data['phone'] = $this->input->post('phone',TRUE); $this->User_Model->add_user($data); } }
private function _form_validation() { $this->form_validation->set_rules('username','trim|required|alpha|callback_check_username'); $this->form_validation->set_rules('realname','trim|required'); $this->form_validation->set_rules('sex','trim|required|numeric'); $this->form_validation->set_rules('email1','trim|required|valid_email'); $this->form_validation->set_rules('phone','trim|required|integer'); return $this->form_validation->run(); }
public function check_username() { echo 'aaaa'; var_dump(__LINE__); exit(); return $this->User_Model->check_username($this->input->post('username')); } }
这里始终打印不出任何内容,而自动跳转到了模型里面
<?phpclass User_Model extends CI_Model {
public function __construct() { parent::__construct(); $this->load->database(); } public function get_pwd_by_username ($username) { $query = $this->db->query("select `name`,`password` from `employee` where `name`={$username}"); return $query->row_array(); } public function check_username($username) { $query = $this->db->query("select name from `employee` where `name`={$username}"); return (empty($query->row))? TRUE : FALSE; } public function add_user($data) { $sql = "insert into `employee` (`name`,`turename`,`sex`,`email1`,`phone`) values({$this->db->escape($data['username'])},{$this->db->escape($data['realname'])},{$this->db->escape($data['sex'])},{$this->db->escape($data['email1'])},{$this->db->escape($data['phone'])})"; $this->db->query($sql); }} |