<?php

    session_start();

    include "../db.php";

    date_default_timezone_set('Asia/Kolkata');

    $currentTime = date('M j, Y g:i a');



    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) ) {

        header("Location: logout.php");

    }

?>

<!DOCTYPE html>

<html lang="en">

<head>

 <?php include "includes/meta.php"; ?>        

    <?php include "includes/css.php"; ?>      

  



</head>

<body id="page-top" class="mt-55">



    <div style="margin-top: 130px;"></div>



      <?php include "includes/header2.php"; ?>

<div class="container">

                                 <?php

                                    if ( isset($_GET["edit_id"]) ) {

                                        $edit_run = mysqli_query($con, "SELECT * FROM subject WHERE sc_id = '$_GET[edit_id]'");

                                        $edit_row = mysqli_fetch_assoc($edit_run);

                                        ?>



                    <div class="row justify-content-center">

                    <div class="col-md-8">

                        <div class="card shadow mt-3">

                        <h5 class="card-header text-dark  text-center font-weight-bold"> Edit Subject</h5>

                            <div class="card-body">

                                <form method="POST">

                                    <div class="row mt-4">

                                        <div class="col-md-12">

                                            <label class="badge badge-warning text-dark">Subject</label>

                                            <input type="text" class=" form-control" id="sc_name" placeholder="Enter Subject" name="sc_name" value="<?php echo $edit_row['sc_name'];?>">

                                        </div>

                                    </div>

                                    <div class=" row justify-content-center">

                                        <button type="submit" class="btn btn-success text-sm-center mt-4" name="upload">

                                            <i class="fa fa-upload"></i> Update

                                        </button>

                                    </div>

                                </form>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

<div style="margin-top: 270px;"></div>

            <?php include "includes/ft.php"; ?>

            <?php include "includes/js.php"; ?>



    

</body>

</html>







<?php



    if(isset($_POST['upload'])){

        $sc_name = mysqli_real_escape_string($con, strip_tags($_POST['sc_name']));

            $up_sql = "UPDATE subject SET sc_name = '$sc_name' WHERE sc_id = '$_GET[edit_id]'";

    

            if(mysqli_query($con, $up_sql)){

                ?>

                <script>window.location = 'view-subject.php';</script>

                <?php

            }

            else{

                ?>

                <script>

                    alert('OOPS !!!!');

                    setTimeout(() => {

                        window.location = 'edit-subject.php?edit_id=<?php echo $_GET["edit_id"]; ?>';

                    }, 1200);

                </script>

                <?php

            }

        } 

    }



?>