无法更新CakePHP中的记录
我有无法更新CakePHP中的记录,php,mysql,cakephp,Php,Mysql,Cakephp,我有答案表格和idplayer\u idread\u status 我正在尝试更新读取状态: $this->Answer->updateAll( array( 'Answer.' . "'" . $data['field'] . "'" => "'" . trim(base64_decode($data['option'])) . "'" ), array( 'Answer.id' => trim(base64_de
答案
表格和id
player\u id
read\u status
我正在尝试更新读取状态
:
$this->Answer->updateAll(
array(
'Answer.' . "'" . $data['field'] . "'" => "'" . trim(base64_decode($data['option'])) . "'"
),
array(
'Answer.id' => trim(base64_decode($data['id']))
)
);
以下字段是动态的,来自AJAX请求:
$data['field']
包含读取状态
$data['option']
包含base64编码的yes
$data['id']
包含base64编码的id
我得到的错误是:
错误:SQLSTATE[42S22]:未找到列:“字段列表”中的1054未知列“应答”“读取状态”SQL查询: 注意:如果要自定义此错误消息,请创建app\View\Errors\pdo_error.ctp
问题似乎是您正在用单引号转义字段名: `回答`.`'read_status`='yes' 应该是 `回答`.`read_status`='yes' 以下更改应修复此问题:
$this->Answer->updateAll(
array('Answer.' . $data['field'] => trim(base64_decode($data['option']))),
array('Answer.id' => trim(base64_decode($data['id'])))
);
问题似乎是您正在用单引号转义字段名: `回答`.`'read_status`='yes' 应该是 `回答`.`read_status`='yes' 以下更改应修复此问题:
$this->Answer->updateAll(
array('Answer.' . $data['field'] => trim(base64_decode($data['option']))),
array('Answer.id' => trim(base64_decode($data['id'])))
);
问题似乎是您正在用单引号转义字段名: `回答`.`'read_status`='yes' 应该是 `回答`.`read_status`='yes' 以下更改应修复此问题:
$this->Answer->updateAll(
array('Answer.' . $data['field'] => trim(base64_decode($data['option']))),
array('Answer.id' => trim(base64_decode($data['id'])))
);
问题似乎是您正在用单引号转义字段名: `回答`.`'read_status`='yes' 应该是 `回答`.`read_status`='yes' 以下更改应修复此问题:
$this->Answer->updateAll(
array('Answer.' . $data['field'] => trim(base64_decode($data['option']))),
array('Answer.id' => trim(base64_decode($data['id'])))
);
试试这个:-
$this->Answer->updateAll(
array(
'Answer.' . $data['field'] => "'" . trim(base64_decode($data['option'])) . "'"
),
array(
'Answer.id' => trim(base64_decode($data['id']))
)
);
试试这个:-
$this->Answer->updateAll(
array(
'Answer.' . $data['field'] => "'" . trim(base64_decode($data['option'])) . "'"
),
array(
'Answer.id' => trim(base64_decode($data['id']))
)
);
试试这个:-
$this->Answer->updateAll(
array(
'Answer.' . $data['field'] => "'" . trim(base64_decode($data['option'])) . "'"
),
array(
'Answer.id' => trim(base64_decode($data['id']))
)
);
试试这个:-
$this->Answer->updateAll(
array(
'Answer.' . $data['field'] => "'" . trim(base64_decode($data['option'])) . "'"
),
array(
'Answer.id' => trim(base64_decode($data['id']))
)
);
不要那样做
您生成的条件是:
WHERE
`Answer`.`id` = 2
没有理由使用updateAll为此,updateAll的主要用例有:
- 更新多个记录
- 非主键更新
- 更新,例如以原子方式递增计数器
$this->Answer->id = $id;
$this->Answer->saveField($field, $val);
或保存:
$this->Answer->id = $id;
$this->Answer->save(array($field => $val));
验证您的输入
如果您选择继续使用问题中的代码,请知道这是危险的
将用户输入直接提供给updateAll允许注入任意SQL。确保数据是您期望的数据:
使用updateAll
调用,没有任何东西可以阻止某人以$field
身份提交:
“读取状态=‘是’和正确=”
实现此sql:
SET `Answer`.`read_status` = 'yes' AND correct = 'yes'
通常,将用户输入放在CakePHP模型数组(更新、条件、顺序)的键中是一个坏主意,因为CakePHP不希望该键具有潜在的恶意,因此不受应用于值的相同转义逻辑的约束
因此,请转义所有用户输入:
$ds = $this->Answer->getDatsource();
$id = (int)trim(base64_decode($data['id']));
$field = $this->Answer->escapeField((base64_decode($data['field']));
$val = $ds->value(trim(base64_decode($data['option']))), $field);
使用上述逻辑并尝试第一个示例中所示的相同注入,结果将是:
SET `Answer`.``read_status` = 'yes' AND correct =` = 'yes';
这只会导致失败,而不会允许用户任意操作数据库
请注意,如果将base64编码作为“安全”措施添加,则它不会起任何作用。不要这样做
您生成的条件是:
WHERE
`Answer`.`id` = 2
没有理由使用updateAll为此,updateAll的主要用例有:
- 更新多个记录
- 非主键更新
- 更新,例如以原子方式递增计数器
$this->Answer->id = $id;
$this->Answer->saveField($field, $val);
或保存:
$this->Answer->id = $id;
$this->Answer->save(array($field => $val));
验证您的输入
如果您选择继续使用问题中的代码,请知道这是危险的
将用户输入直接提供给updateAll允许注入任意SQL。确保数据是您期望的数据:
使用updateAll
调用,没有任何东西可以阻止某人以$field
身份提交:
“读取状态=‘是’和正确=”
实现此sql:
SET `Answer`.`read_status` = 'yes' AND correct = 'yes'
通常,将用户输入放在CakePHP模型数组(更新、条件、顺序)的键中是一个坏主意,因为CakePHP不希望该键具有潜在的恶意,因此不受应用于值的相同转义逻辑的约束
因此,请转义所有用户输入:
$ds = $this->Answer->getDatsource();
$id = (int)trim(base64_decode($data['id']));
$field = $this->Answer->escapeField((base64_decode($data['field']));
$val = $ds->value(trim(base64_decode($data['option']))), $field);
使用上述逻辑并尝试第一个示例中所示的相同注入,结果将是:
SET `Answer`.``read_status` = 'yes' AND correct =` = 'yes';
这只会导致失败,而不会允许用户任意操作数据库
请注意,如果将base64编码作为“安全”措施添加,则它不会起任何作用。不要这样做
您生成的条件是:
WHERE
`Answer`.`id` = 2
没有理由使用updateAll为此,updateAll的主要用例有:
- 更新多个记录
- 非主键更新
- 更新,例如以原子方式递增计数器
$this->Answer->id = $id;
$this->Answer->saveField($field, $val);
或保存:
$this->Answer->id = $id;
$this->Answer->save(array($field => $val));
验证您的输入
如果您选择继续使用问题中的代码,请知道这是危险的
将用户输入直接提供给updateAll允许注入任意SQL。确保数据是您期望的数据:
使用updateAll
调用,没有任何东西可以阻止某人以$field
身份提交:
“读取状态=‘是’和正确=”
实现此sql:
SET `Answer`.`read_status` = 'yes' AND correct = 'yes'
通常,将用户输入放在CakePHP模型数组(更新、条件、顺序)的键中是一个坏主意,因为CakePHP不希望该键具有潜在的恶意,因此不受应用于值的相同转义逻辑的约束
因此,请转义所有用户输入:
$ds = $this->Answer->getDatsource();
$id = (int)trim(base64_decode($data['id']));
$field = $this->Answer->escapeField((base64_decode($data['field']));
$val = $ds->value(trim(base64_decode($data['option']))), $field);
使用上述逻辑并尝试第一个示例中所示的相同注入,结果将是:
SET `Answer`.``read_status` = 'yes' AND correct =` = 'yes';
这只会导致失败,而不会允许用户任意操作数据库
请注意,如果作为“安全”度量添加了base64编码,则为doe