First of all & All above.. User Input Must Be Pass Through Proper Validation & Sanitization
,您可以使用以下控制器&型号举例,让您的结果。
用户控制器:
<?php
// No direct Script execution
defined('BASEPATH') or exit('No direct script access allowed');
/**
* My User Controller
*
* Always make Controller Slim & Model Fat :)
*/
class User extends CI_Controller
{
/**
* Class Constructor
*/
public function __construct()
{
// call parent constructor
parent::__construct();
// load your User_model
$this->load->model('User_model');
}
/**
* If method calling by Via Via...
* @return [type] [description]
*/
public function validate_post(){
// get email from POST
$email = $this->input->post('email', true);
// call another method
$this->get_name($email);
}
/**
* Get User First Name by Email ID
* @return [type] [description]
*/
public function get_name($email)
{
// if user has session email
if ($this->session->userdata('email')) {
// call model method to get first name
$first_name = $this->User_model->get_name($email);
// check what model returns
if ($first_name === false) {
// no record found
show_error('Data error: Supplied email not in record.');
} elseif ($first_name === null) {
// invalid email format
show_error('Validation error: Email is not valid.');
} else {
// Yes! you made it
$this->display('Welcome Back !!! '.$first_name);
}
} else {
// user not login, show them login page
redirect('/');
}
}
}
/* End of file User.php */
/* Location: ./application/controllers/User.php */
User_model型号:
<?php
// No direct script execution
defined('BASEPATH') or exit('No direct script access allowed');
/**
* Class User_model to handle all user related information from MySQL
*/
class User_model extends CI_Model
{
/**
* MySQL table which contains all data about users
* @var string
*/
protected $table = 'sign_up';
/**
* Returns, User First Name by Email ID
* @param [type] $email_addres [description]
* @return [type] [description]
*/
public function get_name($email_addres)
{
// validate email
if (filter_var($email_addres, FILTER_VALIDATE_EMAIL)) {
// desire column from table
$this->db->select('first_name');
// where clause
$this->db->where('email_addres', $email_addres);
// mysql table
$query = $this->db->get($this->table);
// if record exist
if ($query->num_rows() > 0) {
// row, will return first result only, no matter if multiple record exist
$ret = $query->row();
// return first row, firs_name value
return $ret->first_name;
} else {
// error
return false;
}
} else {
// validation error
return null;
}
}
}
/* End of file User_model.php */
/* Location: ./application/models/User_model.php */
的屏幕截图我完成了所有这些 –
@AmaanIqbal,你确定..?你在POST中得到正确的电子邮件,你确定你有在MySQL表中的电子邮件匹配记录,你确定你的查询被创建好,你确定当你干运行你的查询在phpmyadmin你得到的数据,你确定你的表列结构是否正确...?你确定你打的是正确的桌子..你确定你的模型函数被调用.. ?? –
它返回'验证错误:电子邮件无效。 ' –