53 lines
2.0 KiB
PHP
53 lines
2.0 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
|
header("location: login.php");
|
|
exit;
|
|
}
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$item_type = trim($_POST["item_type"]);
|
|
$quantity = trim($_POST["quantity"]);
|
|
$user_id = $_SESSION['user_id'];
|
|
|
|
if (empty($item_type) || empty($quantity) || !is_numeric($quantity) || $quantity <= 0) {
|
|
$_SESSION['error_message'] = "Please enter a valid item and quantity.";
|
|
header("location: dashboard.php");
|
|
exit;
|
|
}
|
|
|
|
// Basic points system: 10 points per item
|
|
$points_awarded = $quantity * 10;
|
|
|
|
try {
|
|
$db = db();
|
|
|
|
// Insert submission
|
|
$sql = "INSERT INTO waste_submissions (user_id, item_type, quantity, points_awarded) VALUES (:user_id, :item_type, :quantity, :points_awarded)";
|
|
$stmt = $db->prepare($sql);
|
|
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
|
|
$stmt->bindParam(':item_type', $item_type, PDO::PARAM_STR);
|
|
$stmt->bindParam(':quantity', $quantity, PDO::PARAM_INT);
|
|
$stmt->bindParam(':points_awarded', $points_awarded, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
|
|
// Update user points
|
|
$sql_update_points = "UPDATE users SET points = points + :points_awarded WHERE id = :user_id";
|
|
$stmt_update_points = $db->prepare($sql_update_points);
|
|
$stmt_update_points->bindParam(':points_awarded', $points_awarded, PDO::PARAM_INT);
|
|
$stmt_update_points->bindParam(':user_id', $user_id, PDO::PARAM_INT);
|
|
$stmt_update_points->execute();
|
|
|
|
$_SESSION['success_message'] = "E-waste submitted successfully! You earned " . $points_awarded . " points.";
|
|
|
|
} catch (PDOException $e) {
|
|
$_SESSION['error_message'] = "Oops! Something went wrong. Please try again later.";
|
|
error_log("E-waste submission failed: " . $e->getMessage());
|
|
}
|
|
|
|
header("location: dashboard.php");
|
|
exit;
|
|
}
|
|
?>
|