当“value”是 varchar(数据库列类型设置为 -> varchar(255)),如“Bush”或“Obama”时,这个带有 PDO 执行语句的 foreach(参见最后一个代码)效果很好:
Array
(
[0] => Array
(
[recid] => 1
[lname] => Bush
)
[1] => Array
(
[recid] => 2
[lname] => Obama
)
)
但是,当“值”获得像“58”或“65”这样的数字(数据库列类型设置为 -> int(11))时,UPDATE 语句将失败,并且没有任何内容更新到数据库中。
Array
(
[0] => Array
(
[recid] => 1
[age] => 58
)
[1] => Array
(
[recid] => 2
[age] => 65
)
)
获取 $_POST 数组和 PDO 执行语句的代码:
foreach ($_POST['changed'] as $SubArray) {
foreach ($SubArray as $key => $value) {
if ($key === 'recid') continue;
$sql = "UPDATE clients SET $key = :value WHERE id = :recid";
$STH = $DBH->prepare($sql);
try {
$STH->execute(array(':value' => $value, ':recid' => $SubArray['recid']));
} catch (PDOException $e) {
$code = $e->getCode();
$file = $e->getFile();
$line = $e->getLine();
$msg = $e->getMessage();
echo "$file:$line ERRNO:$code ERROR:$msg";
}
}
}
我该如何解决这个问题,数据库得到更新?