-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.php
57 lines (47 loc) · 1.71 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
// Start session
session_start();
require 'db_connection.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Ensure all required fields are filled
if (empty($_POST['amt']) || empty($_POST['description']) || empty($_POST['calories']) || empty($_POST['date'])) {
// Handle incomplete form submission
echo "Please fill all the fields.";
exit();
}
// Sanitize and validate user input
$amt = htmlspecialchars($_POST['amt'], ENT_QUOTES);
$description = htmlspecialchars($_POST['description'], ENT_QUOTES);
$calories = htmlspecialchars($_POST['calories'], ENT_QUOTES);
$date = htmlspecialchars($_POST['date'], ENT_QUOTES);
// Validate numeric input
if (!is_numeric($amt) || !is_numeric($calories)) {
// Handle invalid input
echo "Amount and Calories must be numeric values.";
exit();
}
// Retrieve user_id from session
if (!isset($_SESSION['user_id'])) {
// Handle unauthorized access or session expiration
echo "User not logged in.";
exit();
}
$user_id = $_SESSION['user_id'];
// Prepare and execute the SQL query to insert data into the calorie table
$query = "INSERT INTO calorie (user_id, date, amount, calories, description) VALUES (?, ?, ?, ?, ?)";
$statement = $conn->prepare($query);
// Bind parameters
$statement->bind_param("isiss", $user_id, $date, $amt, $calories, $description);
// Execute query
if ($statement->execute()) {
// Redirect to workout.php after successful insertion
header("Location: workout.php");
exit();
} else {
// Error handling
echo "Error: " . $conn->error;
}
}
// Close connection
$conn->close();
?>