gpt4 book ai didi

cakephp - 如何更新Cakephp中的字段

转载 作者:行者123 更新时间:2023-12-04 13:19:43 25 4
gpt4 key购买 nike

我是Cakephp的新手,因此我难以实现数据库查询..我想要的是我想要更新一个手机号码,其中电子邮件等于用户提供的电子邮件。
这是我要实现的查询

        "Update Users set  mobileNo = $mobileNo  where email = $email"

我正在做这个
      $mobileNo = $data['mobileNo'];
$email = $data['$email'];


$count = $this->User->find('count', array(
'conditions' => array('User.email' => $email)));

if($count>0){

$email = $this->User->field(
'email',
array('User.mobileNo' => $mobileNo));
echo $email;

$this->User->saveField("mobileNo",$mobileNo);

最佳答案

CakePHP中的更新通常全部基于了解您要编辑的记录的主键。

普通/简单

更新单个字段的示例为:

$this->User->id = $this->User->field('id', array('email' => $email));
if ($this->User->id) {
$this->User->saveField('mobileNo', $mobileNo);
}

这将导致以下查询(省略了一些计数):
SELECT id from users where email = "$email"
UPDATE users set mobileNo = "$mobileNo" where id = <id>

原子

但是,您可以使用 updateAll执行与问题中显示的查询完全相同的查询:
$this->User->updateAll(
array('mobileNo' => "'$MobileNo'"),
array('email' => $email)
);

这将导致以下查询:
UPDATE users set mobileNo = "$mobileNo" where email = "$email"

如果使用updateAll,请注意,更新应该是sql片段-这意味着您需要引用字符串。

关于cakephp - 如何更新Cakephp中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17316502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com