【问题标题】:Why my fields are not getting added to the database?为什么我的字段没有被添加到数据库中?
【发布时间】:2015-04-05 23:01:40
【问题描述】:

我正在尝试向我的表格列中添加一些字段。当我检查我的表时,我看到可以创建列但无法添加字段。但是,我没有得到任何错误或任何东西。

public function isset_row($target, $sender_table, $receiver_table, $sender_row) {
    $this->load->model('Connection_model');
    if ($this->Connection_model->get_custom_db($target)->get($sender_table)) {

        // Add column(s)
        $this->myforge = $this->load->dbforge($this->Connection_model->get_custom_db('receiver'), TRUE);
        $fields = array(
                $sender_row => array('type' => 'TEXT')
        );
        $this->myforge->add_column($receiver_table, $fields);

        $query = $this->Connection_model->get_custom_db('sender')->get($sender_table);
        foreach ($query->result() as $row) {
            echo $row->$sender_row . '<br>'; // Returns fields from a table (string)
            echo $receiver_table; // Returns table name (string)
            $this->Connection_model->get_custom_db('receiver')->update($receiver_table, $row->$sender_row);
        }
    }
}

编辑1:

var_dump($this->Connection_model->get_custom_db('receiver')->update($receiver_table, $row->$sender_row));

这行返回bool(false)

编辑2:

使用 update() 函数我只是说将字段添加到表中,但我没有说表的哪一列。我认为这一定是重点,但是在尝试更新时如何指定列?

【问题讨论】:

  • 添加字段是什么意思?你的意思是你在表中插入(或更新)一些数据?
  • @ShaifulIslam 是的,完全正确。
  • @RezaSaadati if ($this-&gt;Connection_model-&gt;get_custom_db($target)-&gt;get($sender_table)) 这种情况没有任何意义..
  • @NarendraSisodia 为什么不呢?如果为真,则表示该表存在。

标签: php database codeigniter


【解决方案1】:

完成您的模型 get_custom_db()。在模型中使用 get() 查询构建器方法。所以你的情况应该是这样的

if ($this->Connection_model->get_custom_db($table_name)) {
...
}

并在模型中使用 get()

public function get_custom_db($table_name){
  ....
  $test = $this-db->get($table_name);
  return $test->result();
}

或者提供型号代码,我会帮你搞定的。谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    相关资源
    最近更新 更多