【问题标题】:Database structure possibility数据库结构可能性
【发布时间】:2015-06-04 08:18:04
【问题描述】:

我有一个关于数据库的问题,我正在使用活动记录Codeigniter。

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

第一步:我(用户)使用条件 uniqueID 和 typeofID 对表 A 进行验证。如果该行为 true 或 uniqueID 可用,则返回成功。

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

问题是:我可以将表 B id 插入字段 userID 表 A 中吗? 这样,当有人访问表A时,他通过知道userID就知道表A中的用户名是什么。

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

来自成员的解决方案,已经成功。 首先,我使用 table a uniqueID 将数据插入表 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

【问题讨论】:

    标签: php sql-server database codeigniter odbc


    【解决方案1】:

    插入到表 B 后,使用$user_id = $this-&gt;db-&gt;insert_id() 获取最后一个插入 ID(表 B 的 ID),然后更新表 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));
    

    【讨论】:

    • 对不起,我没有将数据插入到tableA中,我只是检查数据的真假。如果为 false,则验证不会成功。如果成功,那么我可以继续向 tableB 插入数据并更新 tableA。如果,我只是使用 3 行向下(我的意思是插入 tableB 和 insert_id())@rejoanul-alam
    • 所以您可以获得表 A 的行 ID(成功返回)& 现在您的 $last_rowID 将是 $last_rowID = $success[0]['row_ID'] 然后更新表 A。您所要做的就是获取表 A行 ID。现在有想法了吗?
    • 是的,我知道你的意思。但是,表 A 已经填满了数据。我不向其中插入任何数据。那么如果表A数据有27个id,而表B是0呢?想了想,我想这在没有条件的情况下是不可能的。 @rejoanul-alam
    • 因此很明显您的用户 ID 对 tableB 有空/无数据。因此,如果没有将数据插入到表 B 中,请 $last_rowID = null; 希望它现在可以解决
    • 嗨@rejoanul-alam,我已经使用了插入ID。我有问题。为什么我在使用 odbc 时无法获取 id?错误说:&lt;p&gt;Message: Undefined property: CI_DB_odbc_driver::$db&lt;/p&gt; 完全错误在我上面的线程中。
    【解决方案2】:

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

    例如:

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

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

    【讨论】:

    • 我可以先将第一个数据插入到表 B 中,然后将表 ID 插入到列 userID 表 A 中吗? @xammeil
    • 是的,你可以,,,所以这意味着你将在你的表 A 中有一个外键。你需要在表 A 中放置另一列来指定表 B 上的 id 并将其用作表中的连接。
    猜你喜欢
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2013-10-20
    相关资源
    最近更新 更多