gpt4 book ai didi

php - Codeigniter - 插入两个表,第二个表中包含用户 ID

转载 作者:行者123 更新时间:2023-11-29 12:22:48 25 4
gpt4 key购买 nike

我正在尝试插入两个表。在我的第一个表中,我插入了基本的用户信息。在第二个表中,我需要用户 ID 来设置站点的默认文本。例如。 “这是您的第一条文字。您可以点击编辑。”等等

到目前为止我的代码:

//adds the basic user info to the users database
$sql = "INSERT INTO users (first_name, last_name, username, email, password, IP, total_entry, average_entry, date_of_registration, lastactive, account_state)
VALUES (" . $this->db->escape($first_name) . ",
" . $this->db->escape($last_name) . ",
" . $this->db->escape($username) . ",
'" . $email . "',
'" . $password . "',
'" . $IP . "',
'" . $total_entry . "',
'" . $average_entry . "',
'" . $date_of_registration . "',
'" . $lastactive . "',
'" . $account_state . "')";

//adds the basic entry to the database
$data = array(
'message' => $message,
'picture' => $picture,
'uid' => $uid, //problem here, since the $uid is undefined
'time' => $date_of_registration,
);

$this->db->insert('entries', $data);

所以我需要先保存注册用户,然后用用户的 UID 更新第二个表。

codeidniter 中执行此操作的最佳方法是什么?

谢谢

最佳答案

您应该使用 $this->db->insert_id() 作为 $data 数组中的 uid 值,因此,它会类似于:

//将用户基本信息添加到用户数据库

$sql =“插入用户(名字、姓氏、用户名、电子邮件、密码、IP、total_entry、average_entry、date_of_registration、lastactive、account_state) VALUES (". $this->db->e​​scape($first_name) . ", ". $this->db->e​​scape($last_name) . ", ". $this->db->e​​scape($username) . ", '".$email ."', '".$密码 ."', '".$IP ."', '".$total_entry ."', '".$average_entry . "', '".$date_of_registration ."', '".$lastactive ."', '". $account_state . "')";

// Get above inserted id here in variable 

$last_id = $this->db->insert_id();

//adds the basic entry to the database
$data = array(
'message' => $message,
'picture' => $picture,
'uid' => $last_id, //write $last_id here instead $uid
'time' => $date_of_registration,
);

$this->db->insert('entries', $data);

它应该可以工作

关于php - Codeigniter - 插入两个表,第二个表中包含用户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28776657/

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