【发布时间】:2015-10-30 10:15:43
【问题描述】:
我正在尝试使用 PDO 更新 mysql 数据库中表中的一行,并使用 post 方法从表单中获取数据。
例如,此代码无法完成工作(从会话中获取的 ID)...
$u = $_POST;
if( isset($_POST['update']) ) {
$output = 'table';
$usr = "update table set one=?, two=?, three=? where id=?";
$one=$_POST['one'];
$two=$_POST['two'];
$three=$_POST['three'];
$query=$db->prepare($usr);
if( !$query->execute(array($one, $two, $three)) ) {
$db->error;
} else {
print "update successful";
}
}
它也不适用于像这样的四个参数:
$u = $_POST;
if( isset($_POST['update']) ) {
$output = 'table';
$usr = "update table set one=?, two=?, three=? where id=?";
$one=$_POST['one'];
$two=$_POST['two'];
$three=$_POST['three'];
$id=$_POST['id'];
$query=$db->prepare($usr);
if( !$query->execute(array($one, $two, $three, $id)) ) {
$db->error;
} else {
print "update successful";
}
}
这也不起作用(同样,id 取自会话)...
$u = $_POST;
if( isset($_POST['update']) ) {
$output = 'table';
$usr = "update users set one=:one, two=:two, three=:three where id=?";
$res = $db->prepare($usr);
if(!$res->execute(array(':one'=>$u['one'],
':two'=>$u['two'],
':three'=>$u['three']))) {
$error['usr'] = sprintf("%s could not be updated", htmlentities($_POST['firstname']));
$output = 'form'; }
else {
//$status = sprintf("%s created", htmlentities['firstname']);
}
}
我也试过这个http://www.mustbebuilt.co.uk/php/insert-update-and-delete-with-pdo/,它也没有用......
【问题讨论】:
-
确保数据与数据库中已有的数据不同。如果它相同,它将返回 0 行更改
-
如果我在更新它意味着我正在做一些改变,不是吗?......只是在想......
-
其中一个答案发生了什么?...