Поля: имя, фамилия, имя пользователя, адрес электронной почты, пароль и подтверждение пароля.
Однако я получаю следующую ошибку.
Номер ошибки: 1452
Невозможно добавить или обновить дочернюю строку: ограничение внешнего ключа не выполнено (
Код: Выделить всё
lamp.usersКод: Выделить всё
user_idКод: Выделить всё
user_idВСТАВИТЬ В пользователей (
Код: Выделить всё
first_nameИмя файла: models/User_model.php
Номер строки: 33
Я объединил две таблицы users и tweets в моем Tweet_model.php, используя столбцы user_id в каждой таблице.
Структура user_id в обеих таблицах:
Tweets Таблица
Поле: user_id, Тип: INT, Длина: 11, Без знака, Ключ: MUL
Таблица пользователей
Поле: user_id, Тип: INT, Длина: 11, Без знака, Ключ: PRI, Дополнительно: auto_increment
Мой код ниже:
Контроллер: Register.php
Код: Выделить всё
class Register extends CI_Controller
{
public function index()
{
$this->load->model('user_model');
$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('username', 'Username', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('templates/header');
$this->load->view('register');
$this->load->view('templates/footer');
} else {
$this->user_model->set_user();
$this->load->view('templates/header');
$this->load->view('home');
$this->load->view('templates/footer');
}
}
}
Код: Выделить всё
class User_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function get_users($user_id = false)
{
if ($user_id === false) {
$query = $this->db->get('users');
return $query->result_array();
}
$query = $this->db->get_where('users', array('user_id' => $user_id));
return $query->row_array();
}
public function set_user()
{
$url = url_title($this->input->post('user'), 'dash', true);
$data = 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' => $this->input->post('password')
);
return $this->db->insert('users', $data);
}
}
Код: Выделить всё
First Name
Last Name
Username
Email Address
Password
Password Confirm
Код: Выделить всё
class Tweet_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function get_tweets($tweet_id = false)
{
if ($tweet_id === false) {
$this->db->join('users', 'tweets.user_id = users.user_id');
$query = $this->db->get('tweets');
return $query->result_array();
}
$query = $this->db->get_where('tweets', array('tweet_id' => $tweet_id));
return $query->row_array();
}
public function set_tweet()
{
$tweet = url_title($this->input->post('tweet'), 'dash', true);
$data = array(
'tweet' => $this->input->post('tweet')
);
return $this->db->insert('tweets', $data);
}
}
Номер ошибки: 1452
Невозможно добавить или обновить дочернюю строку: ограничение внешнего ключа не выполнено (
Код: Выделить всё
lamp.tweetsКод: Выделить всё
user_idКод: Выделить всё
user_idВСТАВИТЬ В твиты (
Код: Выделить всё
tweetИмя файла: models/Tweet_model.php
Номер строки: 30
Подробнее здесь: https://stackoverflow.com/questions/475 ... straint-fa
Мобильная версия