0
的功能是非常簡單的:PDO更新幫助執行PDO更新
的變量:$表的更新是發生 表和$字段在表中的字段, 和$生成的值從後並投入$值陣列 和$哪裏是表 的索引字段的id的值,$ indxfldnm是索引字段名稱
function SQLUpdate($table,$fields,$values,$where,$indxfldnm) {
//Connect to DB
$dbaddr = DB_HOST;
$dbusr = DB_USER;
$dbpwd = DB_PASSWORD;
$dbname = DB_DATABASE;
$db = new PDO('mysql:host='.$dbaddr .';dbname='.$dbname.';charset=UTF8', $dbusr, $dbpwd);
//build the fields
$buildFields = '';
if (is_array($fields)) {
//loop through all the fields
foreach($fields as $key => $field) :
if ($key == 0) {
//first item
$buildFields .= $field;
} else {
//every other item follows with a ","
$buildFields .= ', '.$field;
}
endforeach;
} else {
//we are only inserting one field
$buildFields .= $fields;
}
//build the values
$buildValues = '';
if (is_array($values)) {
//loop through all the values
foreach($values as $key => $value) :
if ($key == 0) {
//first item
$buildValues .= '?';
} else {
//every other item follows with a ","
$buildValues .= ', ?';
}
endforeach;
} else {
//we are only updating one field
$buildValues .= ':value';
}
$sqlqry = 'UPDATE '.$table.' SET ('.$buildFields.' = '.$buildValues.') WHERE `'.$indxfldnm.'` = \''.$where.'\');';
$prepareUpdate = $db->prepare($sqlqry);
//execute the update for one or many values
if (is_array($values)) {
$prepareUpdate->execute($values);
} else {
$prepareUpdate->execute(array(':value' => $values));
}
//record and print any DB error that may be given
$error = $prepareUpdate->errorInfo();
if ($error[1]) print_r($error);
echo $sqlqry;
return $sqlqry;
}
到目前爲止好 但是其不工作 將值傳遞到正確的更新語句 中的字段時出現錯誤,但我對pdo不太熟悉並將其設置爲 有點幫助,可以修復代碼以將參數綁定到更新中的值 不勝感激
謝謝
我有多個值,它的構建,我需要的字段和值轉換成可以重現「更新」 sqlquery的辦法多個字段。這是問題。我如何構建SQL?把它發送到準備課程? – 2014-12-07 15:25:14