codeigniter - 如何使用不同的数据库表制作注册表单codeigniter
问题描述
我试图在具有不同数据库表的多用户网站上制作注册表单。
例如,当user1 注册时,数据将输入到表“user1”中,当用户2 注册时,将数据输入到表“user2”中。
我只能将 user1 和 user2 输入到同一个表中。这是我使用 role_id 的代码
$data = [
'name' => htmlspecialchars($this->input->post('nama', true)),
'username' => htmlspecialchars($this->input->post('username', true)),
'photo' => 'default.jpg',
'password' => password_hash($this->input->post('password1'), PASSWORD_DEFAULT),
'role_id' => 2,
'is_active' => 1,
'is_date' => time()
];
解决方案
您可以通过应用条件来做到这一点,role_id
但这只有在只有多个用户的情况下才有可能。如果有很多用户并且您想为每个用户创建一个单独的表,请尝试Database Forge Class
(不好的做法)
我已经为您的问题编写了一个可能的解决方案,必要时会提到评论。看看对你有没有帮助。
控制器
$data = [
'name' => htmlspecialchars($this->input->post('nama', true)),
'username' => htmlspecialchars($this->input->post('username', true)),
'photo' => 'default.jpg',
'password' => password_hash($this->input->post('password1'), PASSWORD_DEFAULT),
'role_id' => 2,
'is_active' => 1,
'is_date' => time()
];
$this->xyz_model->xyz_function($data); // load xyz_model and call xyz_function with $data parameters
xyz_model
function xyz_function($data){
$table = 'user1'; // default value
if($data['role_id'] != 1){ // other value is 2 -- assuming only 2 users are there(if not user if else or switch)
$table = 'user2';
}
$this->db->insert($table, $data);
return $this->db->insert_id();
}
推荐阅读
- java - org.geolatte.geom.Point 的 Google Endpoints 转换器
- unity3d - 如何在 Unity 脚本中实现 Thread.Sleep() 功能?
- javascript - 在 JavaScript 中,如何使用 for 循环迭代器编号使对象属性键唯一?
- r - 在标准或给定位置找不到 proj_api.h
- python - Python:在张量流中乘以两个或多个numeric_column
- node.js - 在推送到 res.json 之前进行序列承诺和合并
- windows - 无法使用 WhiteLibrary 切换到 win32 应用程序并进行处理
- android - ARCore 多图像跟踪
- wordpress - 如何处理通过 url 上传到 wordpress 的图片?
- stripe-payments - invoice.payment_succeeded 的身份优先 Stripe webhook