[英]php - update with PDO not working
我正在嘗試使用 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/它也沒有工作......
execute() 方法的參數不是好的參數:
對於第一個示例,預期參數的數量是 4,您只給出 3,缺少 id 參數。
第二,請閱讀文檔,您不能在作為參數給出的關聯數組中設置“:”字符。 id 參數仍然缺失。
你錯過了$id
你只傳遞了 3 個參數
試試這個代碼:-
if( !$query->execute(array($one, $two, $three,$id)) )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.