【问题标题】:Update and Join query together in codeigniter?在codeigniter中一起更新和加入查询?
【发布时间】:2017-07-08 11:56:58
【问题描述】:

在连接两个表时更新数据,但在 where 条件下会出错 我可以在查询中同时使用 join 和 update 吗?

这是我的代码

public function update_model($id,array $data)
{

//$textArea=$data['textdata'];
$this->db->join('user_data', 'user.id = user_data.id');
$this->db>where('user_data.id',$id);
$this->db->update('user_data',$data);

$query=$this->db->get();
return $query->result();
}

我的 mysql 出现如下错误

致命错误:未捕获错误:调用未定义函数 where() in C:\xampp\htdocs\P_Display\application\models\Pmodel.php:103 堆栈 跟踪:#0 C:\xampp\htdocs\P_Display\application\controllers\user.php(1‌​24): Pmodel->update_model('1', Array) #1 C:\xampp\htdocs\P_Display\system\core\CodeIgniter.php(360): 用户->updateSave('1') #2 C:\xampp\htdocs\P_Display\index.php(202): require_once('C:\xampp\htdocs...') #3 {main} 抛出 C:\xampp\htdocs\P_Display\application\models\Pmodel.php 在第 103 行

where 子句给我一个错误,使用此查询是否正确?

【问题讨论】:

  • 上述查询的错误是什么?
  • 致命错误:未捕获错误:调用 C:\xampp\htdocs\P_Display\application\models\Pmodel.php:103 中的未定义函数 where() 堆栈跟踪:#0 C:\xampp\ htdocs\P_Display\application\controllers\user.php(124): Pmodel->update_model('1', Array) #1 C:\xampp\htdocs\P_Display\system\core\CodeIgniter.php(360): User- >updateSave('1') #2 C:\xampp\htdocs\P_Display\index.php(202): require_once('C:\\xampp\\htdocs...') #3 {main} 在 C 中抛出: \xampp\htdocs\P_Display\application\models\Pmodel.php 在第 103 行

标签: php mysql codeigniter


【解决方案1】:

感谢@HekMet 的回复

现在我得到了代码,但以不同的方式查看它,如果你发现它有问题,请告诉我..

 public function update_model($id,array $data)
{
$uname=$data['uname'];
$email=$data['email'];
$password=$data['password'];
$address=$data['address'];
$mobilenumber=$data['Mobilenumber'];
$job=$data['Job'];

$query=
$this->db->set('user_data.email',$email);
$this->db->set('user.password',$password);
$this->db->set('user_data.mobilenumber',$mobilenumber);
$this->db->set('user_data.job',$job);
$this->db->set('user.uname',$uname);

$this->db->where('user_data.id',$id);
$this->db->where('user.id',$id);
$this->db->update('user_data JOIN user ON user_data.id= user.id');


return $query;


}

由于我的变量模棱两可给出错误,我需要单独设置它们的值。

【讨论】:

    【解决方案2】:

    改正为

    $this->db>where('user_data.id',$id);//see here missing arrow
    

    到

    $this->db->where('user_data.id',$id);
    

    更新

    像这样更新表格..

    $sql = "UPDATE user_data AS ud JOIN user AS u ON ud.id = u.id SET ud.col1 = val1,ud.col2 = val2 WHERE ud.id = $id";
    $this->db->query($sql);
    

    或

    $this->db->join('user_data', 'user.id = user_data.id');
    $this->db->set($data);
    $this->db->where('user_data.id',$id);
    $this->db->update('user_data');
    

    【讨论】:

    • 在用户表中的“字段列表”中给出错误未知列“uname”
    • 我认为两个表都有名为uname的字段。对吗?
    • 在设置的条件下可以传数组吗?
    • 使用$this->db->set($array);尝试一次。
    • '->join' 不适用于 codeigniter 查询构建器更新语句
    【解决方案3】:

    $this->db->join() 不包含在$this->db->update() 中,这就是它在查询中出现错误的原因。

    如果你想在更新中使用join(),你必须修改它。

    【讨论】:

      【解决方案4】:

      我有一个类似的问题,我解决它的方法是使用 $this->db->query() 因为在检查我的数据库的日志文件时,生成的 sql 在我使用时会混淆表的别名命令 $this->db->update().

      $sql='UPDATE user_data, user SET uname="' .$uname. '" ,email="' .$email. '" WHERE user_data.id="'.$id.'" AND user.id = user_data.id';
      $this->db->query($sql);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-01
        • 2014-06-12
        • 1970-01-01
        • 1970-01-01
        • 2019-02-15
        相关资源
        最近更新 更多