AECC database project.
1<?php
2print header('Content-Type: application/json');
3include("../../../lib/header.php");
4$_POST = json_decode(file_get_contents("php://input"), true);
5
6function general_delete($table, $id_name) {
7 include("../../../lib/db.php");
8 $out = "";
9 if (isset($_POST[$id_name])) {
10 $stmt = $db -> prepare("DELETE FROM ${table} WHERE ${id_name} = ?;");
11 $stmt -> bind_param("i", $id);
12 $id = validate_input($_POST[$id_name]);
13
14 if ($stmt -> execute()) {
15 print header("HTTP/1.1 204 Succesfully deleted item in ${table} table.");
16 } else {
17 print header("HTTP/1.1 500 Internal server error ocurred while deleting item in ${table} table");
18 }
19 } else {
20 print header("HTTP/1.1 400 Missing ${id_name}");
21 $out = json_encode(err_msg(3));
22 }
23 $db->close();
24
25 return $out;
26}
27
28if (isset($_POST["t"])) {
29 $type = validate_input($_POST["t"]);
30 print match ($type) {
31 "activity" => general_delete("activity", "a_id"),
32 "activity_transaction" => activity_transaction(),
33 "board_member" => general_delete("board_member", "m_id"),
34 "member" => general_delete("member", "m_id"),
35 "product" => general_delete("product", "p_id"),
36 "transaction" => general_delete("transaction", "t_id"),
37 default => header("HTTP/1.1 400 Incorrect table type")
38 };
39} else {
40 print header("HTTP/1.1 400 Missing table type");
41 print json_encode(err_msg(1));
42}
43
44function activity_transaction() {
45 include("../../../lib/db.php");
46 $out = "";
47 if (isset($_POST["a_id"]) && isset($_POST["t_id"])) {
48 $stmt = $db -> prepare("DELETE FROM activity_transaction WHERE a_id = ? and t_id = ?;");
49 $stmt -> bind_param("ii", $a_id, $t_id);
50 $a_id = validate_input($_POST["a_id"]);
51 $t_id = validate_input($_POST["t_id"]);
52
53 if ($stmt -> execute()) {
54 print header("HTTP/1.1 204 Succesfully deleted item in activity_transaction table.");
55 } else {
56 print header("HTTP/1.1 500 Internal server error ocurred while deleting item in activity_transaction table");
57 }
58 } else {
59 $msg = "HTTP/1.1 400 Missing ";
60 if (!isset($_POST["a_id"])) {
61 if (!isset($_POST["t_id"])) {
62 $msg = "a_id and t_id";
63 } else {
64 $msg = "a_id";
65 }
66 } else {
67 $msg = "t_id";
68 }
69 print header($msg);
70 $out = json_encode(err_msg(3));
71 }
72 $db->close();
73
74 return $out;
75}
76?>