1

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

无法添加或更新子行:外键约束失败 ( ecamara. institution_contact_info, CONSTRAINT institution_contact_info_ibfk_2FOREIGN 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;
    }
}
4

2 回答 2

0

它与应用程序代码层关系不大。数据库外键错误是您检查数据库架构并在添加机构之前查看需要哪些依赖项的线索。

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

通过运行弄清楚

显示创建表机构_用户\G

使用 mysql 客户端。也检查 ecamara 表

于 2013-10-01T22:03:37.713 回答
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;
    }
}
于 2013-10-02T10:32:28.797 回答