抓取当前登录用户的用户ID并将其作为外键值添加到另一个表中返回null - MySQL - CodeIgniter

时间:2013-10-01 21:49:59

标签: php mysql codeigniter foreign-keys

我正在尝试根据当前登录用户插入数据。我从会话数据中获取用户电子邮件地址,然后查询用户表中与唯一电子邮件地址匹配的user_id,但在插入时抛出错误号码:1452。

无法添加或更新子行:外键约束失败(ecamarainstitution_contact_info,CONSTRAINT institution_contact_info_ibfk_2 FOREIGN KEY(institution_users_id)参考institution_usersinstitution_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');
    }
}

institution_users_model.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;
    }
}

2 个答案:

答案 0 :(得分:0)

它与应用程序代码层几乎没有关系。数据库外键错误是检查数据库架构的线索,并在添加机构之前查看所需的依赖项。

我猜你在插入用户之前需要定义“机构”记录

通过运行

计算出来
  

SHOW CREATE TABLE institution_users \ G

使用mysql客户端。检查ecamara表

答案 1 :(得分:0)

我必须检查会话数据$ email_address是否有值,然后执行查询并最终遍历数组的元素结果$ institution_users被返回以访问变量$ institution_users_id的值。

这是修改后的函数create_institution_contact();

public function create_institution_contact()
{       
    $email_address = $this->session->userdata('email_address');
    if($email_address){
        $this->db->where('institution_users.email_address', $email_address);
        $institution_users = $this->db->get(institution_users); 
        foreach($institution_users->result() as $row)
        {
            $institution_users_id = $row->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;
    }
}