我正在尝试根据当前登录的用户插入数据。我从会话数据中获取用户电子邮件地址,然后我在用户表中查询与唯一电子邮件地址匹配的 user_id,但在插入时会抛出错误号:1452。
无法添加或更新子行:外键约束失败 ( ecamara
. institution_contact_info
, CONSTRAINT institution_contact_info_ibfk_2
FOREIGN KEY ( institution_users_id
) REFERENCES institution_users
( institution_users_id
))
我一直在想,当我从会话数据中检索电子邮件地址时,它不会返回任何值。
这是我尝试过的;
login.php 控制器
public function login_validate()
{
$this->form_validation->set_rules('email_address', 'Institution Email', 'trim|required|valid_email|callback_validate_credentials');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
//$query = $this->form_validation->run();
if($this->form_validation->run()) //if user credentials validate
{
$data = array(
'email_address' => $this->input->post('email_address'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('login/members_area');
}
else
{
$this->index();
}
}
这是回调函数
public function validate_credentials()
{
$this->load->model('institution_users_model');
if($this->institution_users_model->validate())
{
return true;
}
else
{
$this->form_validation->set_message('validate_credentials', 'Incorrect Email/Password');
return false;
}
}
vetting.php 控制器
public function create_contact()
{
$this->load->model('institution_users_model');
$create_institution_contact = $this->institution_users_model->create_institution_contact();
if($create_institution_contact)
{
redirect('vetting/success_message');
}
else{
redirect('vetting/institution_contact_info');
}
}
机构用户模型.php
public function validate()
{
$this->db->where('email_address', $this->input->post('email_address'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('institution_users');
if($query->num_rows() == 1)
{
return true;
}
else
{
return false;
}
}
public function create_member()
{
$user_data = array(
'institution_name' => $this->input->post('institution_name'),
'email_address' => $this->input->post('email_address'),
'password' => md5($this->input->post('password'))
);
$insert = $this->db->insert('institution_users', $user_data);
if($insert)
{
return true;
}
else
{
return false;
}
}
public function create_institution_contact()
{
$email_address = $this->session->userdata('email_address');
$this->db->select('institution_users.institution_users_id');
$this->db->where('institution_users.email_address', $email_address);
$institution_users = $this->db->get('institution_users');
$result = $institution_users->result();
$institution_users_id = $result[0]->institution_users_id;
$contact_data = array(
'institution_users_id' => '$institution_users_id',
'institution_registration_no' => $this->input->post('institution_registration_no'),
'institution_address' => $this->input->post('institution_address'),
'institution_telephone_no' => $this->input->post('institution_telephone_no'),
'website' => $this->input->post('website')
);
$insert_contact = $this->db->insert('institution_contact_info', $contact_data);
if($insert_contact)
{
return true;
}
else
{
return false;
}
}