我不断收到消息说数据库查询失败::您的SQL语法出错了;检查与MySQL服务器版本对应的手册,以便在第1行的“WHERE id = 2”附近使用正确的语法,我似乎无法找到原因。我的用户插入方法工作正常,但无论出于何种原因,我无法通过此更新方法解决此错误:
public function update(){
global $database;
$sql = "UPDATE users SET ";
$sql .= "username='" . $database->escape_value($this->username) ."', ";
$sql .= "password='" . $database->escape_value($this->password) ."', ";
$sql .= "first_name='" . $database->escape_value($this->first_name) ."', ";
$sql .= "last_name='" . $database->escape_value($this->last_name) ."', ";
$sql .= "WHERE id=" . $database->escape_value($this->id);
$database->query($sql);
if($database->affected_rows() == 1){
$message = "success";
}else{
$message = "no " . mysql_error();
}
return $message;
}
答案 0 :(得分:6)
WHERE
子句之前的最后一个逗号是错误,更改:
$sql .= "last_name='" . $database->escape_value($this->last_name) ."', ";
到
$sql .= "last_name='" . $database->escape_value($this->last_name) ."' ";