简体   繁体   English

在 codeigniter 一起更新和加入查询?

[英]Update and Join query together in codeigniter?

Updating data while join two tables but it gives an error in where condition can i use join and update together in a query?在连接两个表时更新数据,但在我可以在查询中一起使用连接和更新的条件时出现错误?

here is my code这是我的代码

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

I got Error like below in my mysql我的 mysql 出现如下错误

Fatal error: Uncaught Error: Call to undefined function where() in C:\xampp\htdocs\P_Display\application\models\Pmodel.php:103 Stack trace: #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} thrown in C:\xampp\htdocs\P_Display\application\models\Pmodel.php on line 103致命错误:未捕获错误:调用未定义的 function where() in C:\xampp\htdocs\P_Display\application\models\Pmodel.php:103 堆栈跟踪:#0 C:\xampp\htdocs\application\P_docs\P_docs 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 行

the where clause is giving me an error and is it correct to use this query? where 子句给我一个错误,使用这个查询是否正确?

Correct it as 纠正它为

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

TO

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

UPDATE UPDATE

Update table like this.. 像这样更新表..

$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);

OR 要么

$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');

Thanks @HekMet for your reply 感谢@HekMet的回复

now i got the code but in a different way lok at it and let me know if you found some problem with it.. 现在我得到了代码,但以不同的方式了解它并让我知道如果你发现它有一些问题..

 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;


}

since it was giving error that my variable are ambigious i need to just set their values separatly. 因为它给出了我的变量是ambigious的错误,我需要分别设置它们的值。

$this->db->join() is not included in $this->db->update() that is why it came out with an error in query. $this->db->join()不包含在$this->db->update() ,这就是它在查询中出现错误的原因。

If you want to use the join() in the update, you have to modify it. 如果要在更新中使用join() ,则必须对其进行修改。

I had a similar problem and my way to solve it was to use $this->db->query() because when checking the log file of my database the sql that is generated confuses the aliases of the tables when I used the command $this->db->update().我有一个类似的问题,我解决它的方法是使用 $this->db->query() 因为当检查我的数据库的日志文件时,当我使用命令 $ 时生成的 sql 混淆了表的别名这个->数据库->更新()。

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM