-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddplanet.php
More file actions
57 lines (47 loc) · 1.91 KB
/
Copy pathaddplanet.php
File metadata and controls
57 lines (47 loc) · 1.91 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
48
49
50
51
52
53
54
55
56
57
<?php
header('Content-Type: application/json');
// ✅ Database configuration
$host = "localhost:3306";
$user = "root";
$pass = "monish777"; // default password is empty in XAMPP
$db = "COSMIC"; // change if your DB name is different
// 🔌 Connect to the database
$conn = new mysqli($host, $user, $pass, $db);
// ❌ Check connection
if ($conn->connect_error) {
echo json_encode(["success" => false, "error" => "Database connection failed"]);
exit();
}
// ✅ Get POST data from form
$name = $_POST['name'];
$star_id = $_POST['star_id'];
$type = $_POST['type'];
$atmosphere = $_POST['atmosphere'];
$distance = $_POST['distance'];
$orbital_period = $_POST['orbital_period'];
$temperature = $_POST['temperature'];
// ✅ Get the next Planet_ID (find max value and increment by 1)
$sql_max_id = "SELECT MAX(Planet_ID) AS max_id FROM planets";
$result = $conn->query($sql_max_id);
if ($result) {
$row = $result->fetch_assoc();
$new_planet_id = $row['max_id'] + 1; // Increment the max value by 1
} else {
$new_planet_id = 1; // Start from 1 if the table is empty
}
// ✅ Prepare the SQL insert statement (include the manually generated Planet_ID)
$sql = "INSERT INTO planets (Planet_ID, name, star_id, type, atmosphere, distance_from_star, orbital_period, surface_temperature)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
// 🔒 Use prepared statement for security
$stmt = $conn->prepare($sql);
$stmt->bind_param("isisiidd", $new_planet_id, $name, $star_id, $type, $atmosphere, $distance, $orbital_period, $temperature);
// ✅ Execute and respond
if ($stmt->execute()) {
echo json_encode(["success" => true, "message" => "Planet added successfully!"]);
} else {
echo json_encode(["success" => false, "error" => "Failed to add planet: " . $stmt->error]);
}
// 🔚 Close connections
$stmt->close();
$conn->close();
?>