CodeIgniter: Я получаю эту ошибку базы данных 1048 при отправке регистрационной формы.

Я выдаю эту ошибку базы данных на локальном хосте при отправке регистрационной формы при вставке пользовательских значений в базу данных.

Ошибка базы данных Произошла
Номер ошибки: 1048
Столбец ‘first_name’ не может быть пустым
INSERT INTO для пользователей (имя, фамилия, имя пользователя, адрес электронной почты, пароль) ЗНАЧЕНИЯ (NULL, NULL, NULL, », ‘d41d8cd98f00b204e9800998ecf8427e’)

Это мой код (приложение / модель / модель_пользователя)

<?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;
}
}
}

?>

Я также пробовал этот запрос вставки, но все же ошибка БД

 $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;

Ниже приведен файл контроллера (приложение / контроллеры / основной). Нет такой ошибки в этом файле, проверка формы тоже работает правильно.

<?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');
}

}
?>

1

Решение

Попробуй это

В контроллере

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())
{
$this->load->model('model_users');
$result = $this->model_users->add_user();

if($result == TRUE){
$this->load->view('success');
}
else
{
echo 'Sorry, there is a problem with a site. Please try again!' ;
}
}
else {
//Validation failed
$this->load->view('signup');
}
}

В модели

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'));

$data = array(
'first_name' => $this->db->escape($first_name),
'last_name' => $this->db->escape($last_name),
'username' => $this->db->escape($username),
'email' => $this->db->escape($email),
'password' => $this->db->escape($password)
);

if(!$this->db->insert('users', $data))
{
return FALSE;
}
else
{
return TRUE;
}
}
1

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]