Repository navigation
Expand file tree
/
Copy pathsubmit_ticket.php
More file actions
47 lines (40 loc) · 1.81 KB
/
Copy pathsubmit_ticket.php
File metadata and controls
47 lines (40 loc) · 1.81 KB
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
<?php
session_start();
require 'config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['user_id'], $_POST['message'])) {
$user_id = $_SESSION['user_id'];
$message = trim($_POST['message']);
$subject = "Support Ticket";
if (strlen($message) > 500) {
echo json_encode(['success' => false, 'message' => 'Message exceeds 500 characters.']);
exit();
}
$stmt = $pdo->prepare("SELECT id FROM support_tickets WHERE user_id = ? AND status = 'open'");
$stmt->execute([$user_id]);
$ticket = $stmt->fetch();
if ($ticket) {
$ticket_id = $ticket['id'];
$stmt = $pdo->prepare("SELECT COUNT(*) FROM support_replies WHERE ticket_id = ? AND sender = 'user'");
$stmt->execute([$ticket_id]);
$userReplyCount = $stmt->fetchColumn();
if ($userReplyCount >= 3) {
echo json_encode(['success' => false, 'message' => 'Limit of 3 consecutive messages reached. Wait for admin reply.']);
exit();
}
$stmt = $pdo->prepare("INSERT INTO support_replies (ticket_id, sender, message, created_at) VALUES (?, 'user', ?, NOW())");
$stmt->execute([$ticket_id, $message]);
$pdo->prepare("UPDATE support_tickets SET admin_unread = 1 WHERE id = ?")->execute([$ticket_id]);
} else {
$stmt = $pdo->prepare("INSERT INTO support_tickets (user_id, subject, status, admin_unread) VALUES (?, ?, 'open', 1)");
$stmt->execute([$user_id, $subject]);
$ticket_id = $pdo->lastInsertId();
$stmt = $pdo->prepare("INSERT INTO support_replies (ticket_id, sender, message, created_at) VALUES (?, 'user', ?, NOW())");
$stmt->execute([$ticket_id, $message]);
}
header("Location: dashboard.php#support");
exit();
} else {
echo "Error: Missing required fields.";
exit();
}
?>