-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadd-update-plans.php
144 lines (129 loc) · 4.5 KB
/
add-update-plans.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
include 'authenticate.php';
checkUser("admin");
// Include database connection file
include 'db_connect.php';
$message = null;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Sanitize and retrieve form inputs
$plan_name = filter_input(INPUT_POST, 'plan_name', FILTER_SANITIZE_STRING);
$price = filter_input(INPUT_POST, 'price', FILTER_VALIDATE_FLOAT);
$validity_days = filter_input(INPUT_POST, 'validity_days', FILTER_VALIDATE_INT);
$data_per_day = filter_input(INPUT_POST, 'data_per_day', FILTER_VALIDATE_FLOAT);
$talktime = filter_input(INPUT_POST, 'talktime', FILTER_SANITIZE_STRING);
if ($plan_name && $price && $validity_days && $data_per_day && $talktime) {
try { // Check if the plan already exists
$query_check = "SELECT * FROM plans WHERE plan_name = ?";
$stmt_check = $conn->prepare($query_check);
$stmt_check->bind_param("s", $plan_name);
$stmt_check->execute();
$result_check = $stmt_check->get_result();
if ($result_check->num_rows > 0) {
// Plan exists, update the plan
$query_update = "UPDATE plans SET price = ?, validity_days = ?, data_per_day = ?, talktime = ? WHERE plan_name = ?";
$stmt_update = $conn->prepare($query_update);
try {
$stmt_update->bind_param("dids", $price, $validity_days, $data_per_day, $talktime, $plan_name);
if ($stmt_update->execute()) {
$message = "Plan updated successfully.";
} else {
$message = "Error: " . $stmt_update->error;
}
$stmt_update->close();
} catch (Exception $e) {
$message = "Duplicate Plan Name";
}
} else {
// Plan does not exist, insert a new plan
$query_insert = "INSERT INTO plans (plan_name, price, validity_days, data_per_day, talktime) VALUES (?, ?, ?, ?, ?)";
$stmt_insert = $conn->prepare($query_insert);
$stmt_insert->bind_param("sdids", $plan_name, $price, $validity_days, $data_per_day, $talktime);
if ($stmt_insert->execute()) {
$message = "Plan $plan_name with price: $price added successfully.";
} else {
$message = "Error: " . $stmt_insert->error;
}
$stmt_insert->close();
}
$stmt_check->close();
} catch (Exception $e) {
$message = "Duplicate Plan Name";
}
} else {
$message = "All fields are required and must be valid.";
}
// Close database connection
$conn->close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Plans</title>
<link rel="icon" type="image/webp" href="logo.webp" />
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="add-update-plans.css">
</head>
<body>
<header>
<div class="header-top">
<a href="./">
<h1>East Coast Railway</h1>
<h1>Closed User Group</h1>
</a>
</div>
</header>
<main>
<section id="create-plan">
<div class="heading-container">
<button class="back-btn" onclick="window.location.href = './admin-page.php'">
<img src="icon/back-button.webp" alt="back button">
</button>
<h2 class="heading">Add Plan</h2>
</div>
<?php
// Display the message if available
if ($message) {
echo "<p class='session-message'>" . htmlspecialchars($message) . "</p>";
}
?>
<form class="form_container" action="add-update-plans.php" method="post">
<div class="input_box">
<label for="plan_name">Plan Name</label>
<input type="text" id="plan_name" name="plan_name" placeholder="Enter Plan Name" required>
</div>
<div class="input_box">
<label for="price">Price (₹)</label>
<input type="number" step="0.01" id="price" name="price" placeholder="Enter Price" required>
</div>
<div class="input_box">
<label for="validity_days">Validity (days)</label>
<input type="number" id="validity_days" name="validity_days" placeholder="Enter Validity in Days"
required>
</div>
<div class="input_box">
<label for="data_per_day">Data per Day (GB)</label>
<input type="number" step="0.1" id="data_per_day" name="data_per_day"
placeholder="Enter Data per Day" required>
</div>
<div class="input_box">
<label for="talktime">Talktime</label>
<input type="text" id="talktime" name="talktime" placeholder="Enter Talktime Details" required>
</div>
<button class="submit-button" type="submit">
Submit
</button>
</form>
</section>
</main>
<footer>
<p>© 2024 East Coast Railway. All rights reserved.</p>
<div class="footer-links">
<a href="#">Privacy Policy</a>
<a href="#">Terms of Service</a>
</div>
</footer>
</body>
</html>