数据库结构可能性

Database structure possibility

我有一个关于数据库的问题,我正在使用 active recordsCodeigniter。

逻辑是这样的:我有一个table,假设它的名字是A,tableA有4个字段,如:uniqueID,typeofID,date,userID。

第一步:我(用户)使用条件 uniqueID 和 typeofID 使用 table A 进行验证。如果该行为真或 uniqueID 可用,则 returns 成功。

第二步:如果验证成功,那么用户可以将数据插入到一个新的table中,假设table是B。在tableB中有id额外 A.I (自动增加)。在tableB中有4个字段:id、username、question1、question2。

问题是:我可以将table B id 插入到字段userID table A 中吗? 这样,当有人访问tableA时,通过知道userID就可以知道tableA中的username是什么

============================================= ============================

大家的解决方案,已经成功了。 首先,我使用 table uniqueID 将数据插入 table B 并获取最后一个 ID,然后插入数据。

这是我的解决方案

$this->db->insert('tableB', $data);
$result  = $this->db->insert_id();

$this->db->where('uniqueID', $data2['uniqueID']);
$result2 = $this->db->update('tableA', array('idfromtableB' => $result));

return $this->db->affected_rows();

但我在 odbc 中使用 insert_id() 时出错。错误说: 遇到 PHP 错误

严重性:通知

消息:未定义属性:CI_DB_odbc_driver::$db

文件名:odbc/odbc_driver.php

行号:239

回溯:

<p style="margin-left:10px">
        File: C:\xampp\htdocs\myproject\application\models\users_m.php
    <br />
        Line: 79
    <br />
        Function: insert_id         
</p>

============================================= ============================

在此处找到解决方案:SQL Server last insert id return array in CodeIgniter

是的,你只需要加入你的桌子:

例如:

public function get_info($id){
  $this->db->select('*');
  $this->db->from('tableA');
  $this->db->join('tableB' ,tableA.id=tableB.id);
  $this->db->where('tableA.id' , $id);
  $query = $this->db->get();

  return $query->result_array(); 

}

请修改它以适合您的代码。

插入到table B后,使用$user_id = $this->db->insert_id()获取最后插入的ID(table B的ID)然后更新table A。所以你的代码看起来喜欢

...
$this->db->insert('tableA',$data);
$last_rowID = $this->db->insert_id();
$this->db->insert('tableB',$dataB);
$user_id = $this->db->insert_id();
$this->db->where('tableA_ID',$last_rowID )->update('tableA',array('userID' => $user_id));