Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function insert($table, $columnsArray, $requiredColumnsArray) {
$this->verifyRequiredParams($columnsArray, $requiredColumnsArray);
try{
$a = array();
$c = "";
$v = "";
foreach ($columnsArray as $key => $value) {
$c .= $key. ", ";
$v .= ":".$key. ", ";
$a[":".$key] = $value;
}
$c = rtrim($c,', ');
$v = rtrim($v,', ');
$stmt = $this->db->prepare("INSERT INTO $table($c) VALUES($v)");
$stmt->execute($a);
$affected_rows = $stmt->rowCount();
$response["status"] = "success";
$response["message"] = $affected_rows." row inserted into database";
}catch(PDOException $e){
$response["status"] = "error";
$response["message"] = 'Insert Failed: ' .$e->getMessage();
}
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.