Skip to content

Instantly share code, notes, and snippets.

@itswadesh
Created March 27, 2017 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itswadesh/b295e19cb9fcb3d3e36b60b3fb7573b8 to your computer and use it in GitHub Desktop.
Save itswadesh/b295e19cb9fcb3d3e36b60b3fb7573b8 to your computer and use it in GitHub Desktop.
function update($table, $columnsArray, $where, $requiredColumnsArray){
$this->verifyRequiredParams($columnsArray, $requiredColumnsArray);
try{
$a = array();
$w = "";
$c = "";
foreach ($where as $key => $value) {
$w .= " and " .$key. " = :".$key;
$a[":".$key] = $value;
}
foreach ($columnsArray as $key => $value) {
$c .= $key. " = :".$key.", ";
$a[":".$key] = $value;
}
$c = rtrim($c,", ");
$stmt = $this->db->prepare("UPDATE $table SET $c WHERE 1=1 ".$w);
$stmt->execute($a);
$affected_rows = $stmt->rowCount();
if($affected_rows<=0){
$response["status"] = "warning";
$response["message"] = "No row updated";
}else{
$response["status"] = "success";
$response["message"] = $affected_rows." row(s) updated in database";
}
}catch(PDOException $e){
$response["status"] = "error";
$response["message"] = "Update Failed: " .$e->getMessage();
}
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment