Mini Kabibi Habibi

Current Path : C:/xampp/htdocs/dcp_inventory/
Upload File :
Current File : C:/xampp/htdocs/dcp_inventory/edit_inventory.php

<?php
include 'db_connection.php';
session_start();

// Check if 'id' parameter is in the URL
if (isset($_GET['id'])) {
    $id = $_GET['id'];

    // Fetch the inventory record based on the ID
    $sql = "SELECT * FROM submitted_inventory WHERE id = ?";
    $stmt = mysqli_prepare($conn, $sql);
    mysqli_stmt_bind_param($stmt, 'i', $id);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);

    if ($row = mysqli_fetch_assoc($result)) {
        // Assign values to variables
        $school_id = $row['school_id'];
        $name_of_school = $row['name_of_school'];
        $district = $row['district'];
        $dcp_batch_number_received = $row['dcp_batch_number_received'];
        $quantity = $row['quantity'];
        $supplier = $row['supplier'];
        $date_received = $row['date_received'];
        $current_status = $row['current_status'];
    } else {
        echo "Record not found.";
        exit();
    }
} else {
    echo "No ID parameter found in the URL.";
    exit();
}

// Handle form submission and updating the record
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Collect POST data and sanitize it
    $updated_school_id = mysqli_real_escape_string($conn, $_POST['school_id']);
    $updated_name_of_school = mysqli_real_escape_string($conn, $_POST['name_of_school']);
    $updated_district = mysqli_real_escape_string($conn, $_POST['district']);
    $updated_dcp_batch_number_received = mysqli_real_escape_string($conn, $_POST['dcp_batch_number_received']);
    $updated_quantity = mysqli_real_escape_string($conn, $_POST['quantity']);
    $updated_supplier = mysqli_real_escape_string($conn, $_POST['supplier']);
    $updated_date_received = mysqli_real_escape_string($conn, $_POST['date_received']);
    $updated_current_status = mysqli_real_escape_string($conn, $_POST['current_status']);

    // Update query
    $update_sql = "UPDATE submitted_inventory SET 
                    school_id = ?, 
                    name_of_school = ?, 
                    district = ?, 
                    dcp_batch_number_received = ?, 
                    quantity = ?, 
                    supplier = ?, 
                    date_received = ?, 
                    current_status = ? 
                    WHERE id = ?";
    $stmt_update = mysqli_prepare($conn, $update_sql);
    mysqli_stmt_bind_param($stmt_update, 'ssssisssi', $updated_school_id, $updated_name_of_school, $updated_district, $updated_dcp_batch_number_received, $updated_quantity, $updated_supplier, $updated_date_received, $updated_current_status, $id);
    $update_success = mysqli_stmt_execute($stmt_update);

    // Set success or error message in session
    if ($update_success) {
        $_SESSION['success_message'] = "Inventory updated successfully!";
    } else {
        $_SESSION['error_message'] = "There was an error updating the inventory.";
    }

    // Redirect after update
    header("Location: school_dashboard.php");
    exit();
}
?>

<!-- HTML Form for editing the inventory -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Inventory</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }
        .container {
            width: 50%;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            margin-top: 30px;
        }
        h2 {
            text-align: center;
            color: #333;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        input, select, button {
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        button {
            background-color: #28a745;
            color: white;
            cursor: pointer;
        }
        button:hover {
            background-color: #218838;
        }
        .message {
            padding: 10px;
            margin-bottom: 20px;
            border-radius: 4px;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>
<body>

<div class="container">
    <h2>Edit Inventory</h2>

    <!-- Display success or error message -->
    <?php if (isset($_SESSION['success_message'])): ?>
        <div class="message success">
            <?php echo $_SESSION['success_message']; unset($_SESSION['success_message']); ?>
        </div>
    <?php endif; ?>
    <?php if (isset($_SESSION['error_message'])): ?>
        <div class="message error">
            <?php echo $_SESSION['error_message']; unset($_SESSION['error_message']); ?>
        </div>
    <?php endif; ?>

    <form method="POST">
        <input type="text" name="school_id" value="<?php echo htmlspecialchars($school_id); ?>" readonly>
        <input type="text" name="name_of_school" value="<?php echo htmlspecialchars($name_of_school); ?>" required>
        <input type="text" name="district" value="<?php echo htmlspecialchars($district); ?>" required>
        <input type="text" name="dcp_batch_number_received" value="<?php echo htmlspecialchars($dcp_batch_number_received); ?>" required>
        <input type="number" name="quantity" value="<?php echo htmlspecialchars($quantity); ?>" required>
        <input type="text" name="supplier" value="<?php echo htmlspecialchars($supplier); ?>" required>
        <input type="date" name="date_received" value="<?php echo htmlspecialchars($date_received); ?>" required>
        <select name="current_status" required>
            <option value="Functional" <?php echo $current_status == 'Functional' ? 'selected' : ''; ?>>Functional</option>
            <option value="Not Functional" <?php echo $current_status == 'Not Functional' ? 'selected' : ''; ?>>Not Functional</option>
            <option value="Need Repair" <?php echo $current_status == 'Need Repair' ? 'selected' : ''; ?>>Need Repair</option>
            <option value="Stolen" <?php echo $current_status == 'Stolen' ? 'selected' : ''; ?>>Stolen</option>
        </select>
        <button type="submit">Update Inventory</button>
    </form>
</div>

</body>
</html>