在阅读一本书时,我遇到了以下功能:
/*
Update records in the database
@param String $table the table being updated
@param Array $changes array of changes field => value
@param String $condition the condition
@return Boolean
*/
public function updateRecords($table, array $changes, $condition)
{
$update = "UPDATE " . $table . " SET ";
foreach($changes as $field => $value)
{
$update .= "`" . $field . "` = '{$value}', ";
}
//remove trailing , (comma)
$update .= substr($update, 0, -1);
if($condition != '')
{
$update .= "WHERE " . $condition;
}
$this->executeQuery($update);
//Not sure why it returns true.
return true;
}
如果我错了,请纠正我,但这不是一个设计糟糕的功能,绝对没有数据过滤/检查。最重要的是,该函数总是返回“真”。