function update_field($table, $prefix, $field_name) { global $db; if (!array_key_exists('id', $_POST) || !array_key_exists($field_name, $_POST)) { return FALSE; } $id = intval($_POST['id']); $prefixed_field_name = $prefix . $field_name; $id_field_name = $prefix ? $prefix . '_id' : 'id'; execute_query_and_expect_one_row_to_be_affected("UPDATE {$table} SET {$prefixed_field_name} = \$2 WHERE {$id_field_name} = \$1", array($id, $_POST[$field_name]), "Updated the {$prefix} {$id}", "Could not update {$prefix} {$id}"); return TRUE; }
function update_field($table, $prefix, $field_name, $new_value = NULL) { global $db; if (!array_key_exists('id', $_POST)) { return FALSE; } $id = intval($_POST['id']); $prefixed_field_name = $prefix . '_' . $field_name; $id_field_name = $prefix . '_id'; if ($new_value == NULL) { if (array_get($_POST, 'updated-column') != $field_name && !array_key_exists($field_name, $_POST)) { return FALSE; } $new_value = array_get($_POST, $field_name); } execute_query_and_expect_one_row_to_be_affected("UPDATE {$table} SET {$prefixed_field_name} = \$2 WHERE {$id_field_name} = \$1", array($id, $new_value), "Updated the {$prefix} {$id}", "Could not update {$prefix} {$id}"); return TRUE; }
<?php require '../include/admin-header.php'; if ($action == 'delete') { if (array_key_exists('id', $_POST)) { $job_id = intval($_POST['id']); execute_query_and_expect_one_row_to_be_affected('DELETE FROM jobs WHERE job_id = $1', array($job_id), "Deleted job {$job_id}", "Could not delete job {$job_id}"); } else { notice('Invalid ID.'); } } else { if ($action == 'manifest') { execute_query_and_expect_one_row_to_be_affected('INSERT INTO jobs (job_type) VALUES (\'manifest\')', array(), 'Requested to regenerate the manifest.', 'Could not add a job'); } } if ($db) { ?> <table> <thead> <tr><td>ID</td><td>Type</td><td>Created At</td><td>Started At</td><td>PID</td><td>Attempts</td><td>Payload</td><td>Log</td><td>Actions</td> </thead> <tbody> <?php function get_value_with_default($array, $key, $default) { $value = $array[$key]; if (!$value) { $value = $default; } return $value; }