1
我在提交註冊表單的同時將用戶值插入數據庫時,在本地主機上發出此數據庫錯誤。CodeIgniter:我在提交註冊表單時發生此數據庫錯誤1048。
數據庫出錯 錯誤號碼:1048 列 'FIRST_NAME' 不能爲空 INSERT INTO用戶(名字,姓氏,用戶名,電子郵件,密碼)VALUES(NULL,NULL,NULL, '',「d41d8cd98f00b204e9800998ecf8427e 「)
這是我的代碼(應用程序/模型/ model_user)
<?php
class Model_users extends CI_Model{
public function can_log_in(){
$this->db->where('email', $this->input->post('email'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('users');
if($query->num_rows() == 1){
return TRUE;
}
else {
return FALSE;
}
}
public function add_user(){
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$username = $this->input->post('username');
$email = $this->input->post('email');
$password = md5($this->input->post('password'));
$sql = "INSERT INTO users(first_name, last_name, username, email, password)
VALUES(" .$this->db->escape($first_name). ",
" .$this->db->escape($last_name). ",
" .$this->db->escape($username). ",
'".$email."',
'".$password."')";
$query = $this->db->query($sql);
if($query){
return TRUE;
}
else{
return FALSE;
}
}
}
?>
我也試過這種插入查詢,但尚未同DB錯誤
$new_members = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'password' => md5($this->input->post('password'))
);
$insert = $this->db->insert('users', $new_members);
if($insert){
return TRUE;
} else {
return FALSE;
以下是控制器文件(application/controllers/main)。在這個文件中沒有這樣的錯誤,form-validation也能正常工作。
<?php
class Main extends CI_Controller{
public function index() {
$this->login();
}
public function login(){
$this->load->view('login');
}
public function signup(){
$this->load->view('signup');
}
public function members(){
if($this->session->userdata('is_logged_in')){
$this->load->view('members');
}
else {
redirect('main/restricted');
}
}
public function restricted(){
$this->load->view('restricted');
}
public function login_validation(){
$this->load->helper('security');
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','required|trim|xss_clean|callback_validate_credentials');
$this->form_validation->set_rules('password','Password','required|md5|trim');
if($this->form_validation->run()){
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => 1
);
$this->session->set_userdata($data);
redirect('main/members');
}
else{
$this->load->view('login');
}
}
public function signup_validation(){
$this->load->helper('security');
$this->load->library('form_validation');
$this->form_validation->set_rules('first_name','First Name','required');
$this->form_validation->set_rules('last_name','Last Name','required');
$this->form_validation->set_rules('email','Email','required|trim|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('username','Username','required|trim|is_unique[users.username]');
$this->form_validation->set_rules('password','Password','required|trim');
$this->form_validation->set_rules('cpassword','Confirm Password','required|trim|matches[password]');
$this->form_validation->set_message('is_unique','That email address already exist!');
if($this->form_validation->run()){
//echo 'Validation Successful.';
redirect('main/add_user');
}
else {
//Validation failed
$this->load->view('signup');
}
}
public function add_user(){
$this->load->model('model_users');
$result = $this->model_users->add_user();
if($result){
$this->load->view('success');
}
else{ //this should never happen
echo 'Sorry, there is a problem with a site. Please try again!' ;
}
}
public function validate_credentials(){
$this->load->model('model_users');
if($this->model_users->can_log_in()){
return TRUE;
}
else{
$this->form_validation->set_message('validate_credentials','Sorry incorrect Email ID or Password');
return FALSE ;
}
}
public function logout(){
$this->session->sess_destroy();
redirect('main/login');
}
}
?>
;'輸入數據不承認模型 –
你是passi ng值,這就是它的意思。你有沒有放置任何主鍵?例如:電子郵件,手機?如果是這樣,不要傳遞null,嘗試傳遞一些實際值。 –
@HardikPatil檢查下面的答案 –