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你得到的數據,你確定你的表列結構是否正確...?你確定你打的是正確的桌子..你確定你的模型函數被調用.. ?? –
它返回'驗證錯誤:電子郵件無效。 ' –