<?php 
session_start();
include("connection/conn.php");


// Initialize variables
$id = 0;
$row = ['subject_name' => '']; // Default values to avoid errors

// Fetch the subject details for editing
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
    $id = intval($_GET['id']);
    $query = "SELECT * FROM subjects WHERE id = ?";
    $stmt = mysqli_prepare($link, $query);
    mysqli_stmt_bind_param($stmt, "i", $id);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    
    if ($row = mysqli_fetch_assoc($result)) {
        // Data fetched successfully
    } else {
        echo "<script>alert('Subject not found!'); window.location='manage-subjects.php';</script>";
        exit();
    }
}

// Update the subject if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['update'])) {
    $subject_name = trim($_POST['subject_name']);

    // Validate input
    if (!empty($subject_name)) {
        $update_query = "UPDATE subjects SET subject_name = ? WHERE id = ?";
        $stmt = mysqli_prepare($link, $update_query);
        mysqli_stmt_bind_param($stmt, "si", $subject_name, $id);
        
        if (mysqli_stmt_execute($stmt)) {
            header("Location: manage-subjects.php");
            exit();
        } else {
            echo "<script>alert('Error updating subject');</script>";
        }
    } else {
        echo "<script>alert('Subject name cannot be empty');</script>";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Administrator | Update Category</title>
    <link href="css/bootstrap.css" rel="stylesheet" />
    <link href="css/style.css" rel="stylesheet" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>

<body>
    <div id="wrapper">
        <?php
            include("top-header.php");
            include("right-sec.php");
        ?>
        	<div id="page-wrapper" >
				<div id="page-inner">
					<div class="row">
						<div class="col-md-12">
							<h3 class="page-header">
								Update Category  <small></small>
							</h3>
							<div class="table-sec">
								<div class="srch_box">
									<form name="pnews" id="pnews" method="post" action="" enctype="multipart/form-data">
    <div class="row">
        <div class="col-md-12 col-xs-12">
            <div class="pnl_box"> 
                <div class="col-md-12 col-xs-6">    
                    <div class="form-group form-select">
                        <label>Course Category <span>*</span></label>
                        <select name="course_category" id="course_category" class="form-control">
                            <option value="">Select Category</option>
                            <?php
                            $sqlcourse="SELECT * FROM course_category ORDER BY id ASC";
                            $resultcourse=mysqli_query($link,$sqlcourse);
                            while($findcourse=mysqli_fetch_array($resultcourse)){ ?>
                                <option value="<?php echo $findcourse['id']; ?>"><?php echo $findcourse['course']; ?></option>
                            <?php } ?>
                        </select>
                    </div>        
                </div>
                <div class="col-md-12 col-xs-6">    
                    <div class="form-group">
                        <label>Course Name <span>*</span></label>
                        <input type="text" id="subject" class="form-control" name="subject_name" value="<?php echo htmlspecialchars($row['subject_name']); ?>" required>
                    </div>        
                </div>
            </div>    
        </div>
    </div>
    <div class="row">
        <div class="col-md-4 col-xs-4">    
            <div class="btn_sec">
                <input type="submit" name="update" value="Submit" />
            </div>
        </div>
    </div>
</form>















								</div>
							</div>
						</div>
					</div> 
				</div>
			</div>
        
      
        <footer class="footer-sec">
            <div class="container">
                <div class="row">
                    <div class="col-md-12">
                        <p class="text-center">All rights reserved. Template Design by: <a href="">Anshul Chittouria</a></p>
                    </div>
                </div>
            </div>
        </footer>
    </div>
    
    <!-- jQuery Js -->
    <script src="js/jquery-1.10.2.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery.metisMenu.js"></script>
    <script src="js/custom-scripts.js"></script>
</body>
</html>
