gpt4 book ai didi

php - 获取当前登录用户的用户 ID 并将其作为外键值添加到不同的表中返回 null - MySQL - CodeIgniter

转载 作者:行者123 更新时间:2023-11-30 23:13:01 24 4
gpt4 key购买 nike

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

无法添加或更新子行:外键约束失败 (ecamara.institution_contact_info, CONSTRAINT institution_contact_info_ibfk_2 FOREIGN KEY ( institution_users_id) 引用资料 institution_users (institution_users_id))

我一直在想,当我从 session 数据中检索电子邮件地址时,它确实不返回任何值。

这是我试过的;

login.php Controller

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 Controller

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

最佳答案

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

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

通过运行找出它

SHOW CREATE TABLE institution_users\G

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

关于php - 获取当前登录用户的用户 ID 并将其作为外键值添加到不同的表中返回 null - MySQL - CodeIgniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19126586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com