-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_post.php
35 lines (30 loc) · 1.32 KB
/
api_post.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
<?php
include ('config.php');
// Validate input
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST["id"]) && isset($_POST["txt"])) {
$id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_STRING);
$tx = filter_input(INPUT_POST, 'txt', FILTER_SANITIZE_STRING);
// Prepare SQL queries with prepared statements
$stmt = $conn->prepare('SELECT text FROM sharecode WHERE id = ?');
$stmt->bind_param('s', $id);
$stmt->execute();
$result = $stmt->get_result();
if ($tx != "" && $result->num_rows == 0) {
$stmt = $conn->prepare('INSERT INTO `sharecode`(`id`, `text`, `last_edit`) VALUES(?, ?, ?)');
$stmt->bind_param('sss', $id, $tx, time());
$stmt->execute();
} else if ($tx == "") {
$stmt = $conn->prepare('DELETE FROM `sharecode` WHERE id = ?');
$stmt->bind_param('s', $id);
$stmt->execute();
} else {
$stmt = $conn->prepare('UPDATE sharecode SET text = ?, last_edit = ? WHERE id = ?');
$stmt->bind_param('ssi', $tx, time(), $id);
$stmt->execute();
}
echo json_encode(array("status" => "success", "message" => "Text uploaded successfully"));
} else {
// Return error response if the required parameters are not provided
echo json_encode(array("status" => "error", "message" => "A problem arises when uploading.."));
}
?>