-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddasteroid.php
More file actions
50 lines (42 loc) · 1.57 KB
/
addasteroid.php
File metadata and controls
50 lines (42 loc) · 1.57 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
<?php
$servername = "localhost:3306";
$username = "root";
$password = "monish777";
$dbname = "COSMIC";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Validate required fields
if (
isset($_POST['name']) && isset($_POST['galaxy_id']) && isset($_POST['composition']) &&
isset($_POST['diameter']) && isset($_POST['velocity']) && isset($_POST['closest_approach'])
) {
$name = $_POST['name'];
$galaxy_id = $_POST['galaxy_id'];
$composition = $_POST['composition'];
$diameter = $_POST['diameter'];
$velocity = $_POST['velocity'];
$closest_approach = $_POST['closest_approach'];
// Generate new Asteroid_ID manually
$result = $conn->query("SELECT MAX(Asteroid_ID) AS max_id FROM Asteroids");
$row = $result->fetch_assoc();
$new_id = $row['max_id'] + 1;
if ($new_id === null) $new_id = 1; // If table is empty
// Prepare insert statement
$stmt = $conn->prepare("INSERT INTO Asteroids (Asteroid_ID, Name, Galaxy_ID, Composition, Diameter, Velocity, Closest_Approach_To_Earth)
VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("isissdd", $new_id, $name, $galaxy_id, $composition, $diameter, $velocity, $closest_approach);
if ($stmt->execute()) {
echo "✅ Asteroid added successfully with ID: $new_id";
} else {
echo "❌ Error: " . $stmt->error;
}
$stmt->close();
} else {
echo "❗ Missing required input values.";
}
$conn->close();
?>