-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdelete.php
67 lines (56 loc) · 2.42 KB
/
delete.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
require('configuration/connection.php');
require('configuration/functions.php');
require('configuration/custom-functions.php');
/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! WARNING: Do not modify this file! Any changes may cause errors !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*******************************************************************
* This script was developed by Imdadullah Babu *
* Website: https://imdos.in *
* Organization: Pen Programmer (https://penprogrammer.com) *
*******************************************************************
*/
// Check if the request method is POST and content type is JSON
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || empty($_SERVER['CONTENT_TYPE']) || $_SERVER['CONTENT_TYPE'] !== 'application/json') {
http_response_code(405);
echo json_encode(['status' => 'error', 'message' => 'Invalid request method or content type']);
exit;
}
// Disable autocommit for database transactions
$conn->autocommit(false);
try {
// Decode JSON data from request body
$requestData = json_decode(file_get_contents('php://input'), true, 512, JSON_THROW_ON_ERROR);
// Check if 'table' parameter exists in the request data
if (!isset($requestData['table'])) {
throw new Exception('Missing required parameters: table');
}
// Check if 'conditions' parameter exists in the request data
if (!isset($requestData['conditions'])) {
throw new Exception('Missing required parameters: conditions');
}
// Prepare SQL query for deletion
$table = $requestData['table'];
$conditions = buildWhereClause($conn, $requestData['conditions']);
$sql = "DELETE FROM $table $conditions";
// Execute the SQL query
$result = $conn->query($sql);
// If query executed successfully, commit transaction and return success response
if ($result) {
$conn->commit();
http_response_code(200);
echo json_encode(['status' => 'success', 'message' => 'Data deleted successfully']);
} else {
throw new Exception('Delete query failed');
}
} catch (Exception $e) {
// Rollback the transaction on any exception
$conn->rollback();
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
} finally {
// Close the database connection
$conn->close();
}