Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter,按变量中包含的值增加数据库值_Php_Codeigniter_Activerecord - Fatal编程技术网

Php Codeigniter,按变量中包含的值增加数据库值

Php Codeigniter,按变量中包含的值增加数据库值,php,codeigniter,activerecord,Php,Codeigniter,Activerecord,我正在使用codeigniter,在我的模型中我有以下功能来给用户打分。 但是,它不起作用,而是将“点”列设置为0 代码点火器手册就是这样写的。所以我不知道为什么它不起作用 谢谢 function give_points($username,$points) { $this->db->set('points', 'points + $points'); $this->db->where('username', $username); $this-&

我正在使用codeigniter,在我的模型中我有以下功能来给用户打分。 但是,它不起作用,而是将“点”列设置为0

代码点火器手册就是这样写的。所以我不知道为什么它不起作用

谢谢

function give_points($username,$points)
{
    $this->db->set('points', 'points + $points');
    $this->db->where('username', $username);
    $this->db->update('users'); 
    echo"done";
}

不确定这是问题的原因,但在以下行中使用了单引号:

$this->db->set('points', 'points + $points');
这样,
$points
字符串将按原样注入到SQL查询中——将使用的不是它的值


如果要对
$points
进行插值(以便将其值放在该字符串中的相应位置),则必须使用双引号:

$this->db->set('points', "points + $points");


有关变量插值的更多信息,请参阅PHP手册的一节。

如果有机会,请始终检查创建的SQL查询-我不知道如何使用CI

但是,您的
set()
看起来有缺陷

$this->db->set('points', "points + $points");
以前,
$points
字符串的一部分,由于使用单引号而不是双引号,因此未通过
$points
的内容进行扩展-请参阅PHP中的


稍好一点的代码是上面的代码,因为它可能会失败,这取决于
$points
最初来自哪里。

我相信您必须明确告诉CI不要转义文本。我手头没有一个CI安装来测试这一点,但我认为它类似于:

$this->db->set('points', 'points + ' . (int) $points, FALSE);

这两个建议都会产生查询:更新
用户
设置
点数
='points+1000',其中
用户名
='thomas'此查询不起作用。。。谢谢,那么你必须检查CI手册。似乎CI总是将
set()
中的给定字符串放在引号中,因此这无法工作。这产生了以下查询:UPDATE
users
set
points
='points+1000',其中
username
='thomas'不起作用。。还有什么想法吗?看起来数据库中的
字段是一个整数,您试图像字符串一样访问它:您的查询应该像
更新用户设置点='points+1000',其中username='thomas'
;;;也许你有办法向CI表明这一点?对不起,我对CI不太了解,无法提供更多帮助。
$this->db->set('points', 'points + ' . (int) $points, FALSE);