function remove_image($connection, $id) { remove_image_file(get_image($connection, $id)); $delete_image_by_id_stmt = delete_image_by_id_stmt($connection); $delete_image_by_id_stmt->bind_param("i", $id); catch_execution_error($delete_image_by_id_stmt->execute(), $connection); }
function save_image($connection) { $file = $_FILES["image"]; $image_id = $_REQUEST['imageId']; $title = $_REQUEST['title']; $section_id = $_REQUEST['sectionId']; $file_path = basename($file["name"]); $connection->begin_transaction(); if ($image_id > 0) { $old_image = get_image($connection, $id); if ($file_path != "" && $old_image != $file_path) { remove_image_file($old_image); do_file_move($file); $update_section_image_stmt = update_section_image_stmt($connection); $update_section_image_stmt->bind_param("ssi", $file_path, $title, $id); catch_execution_error($update_section_image_stmt->execute(), $connection); } else { $update_section_image_stmt = update_section_image_stmt($connection); $update_section_image_stmt->bind_param("ssi", $old_image, $title, $id); catch_execution_error($update_section_image_stmt->execute(), $connection); } print '[["SUCCESS"],["Image saved successfully"]]'; } else { do_file_move($file); $next = get_next_order($connection, "section_to_image"); $insert_section_image_stmt = insert_section_image_stmt($connection); $insert_section_image_stmt->bind_param("iiss", $section_id, $next, $file_path, $title); catch_execution_error($insert_section_image_stmt->execute(), $connection); print "[[\"SUCCESS\"],{\"id\": " . get_last_inserted_id($connection) . ", \"sectionId\": {$section_id}, \"title\": \"{$title}\", \"filePath\": \"{$file_path}\"}]"; } $connection->commit(); }