<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<!--
    CS 328 - Week 12 Lecture 1 - postback PHP that either generates a form for
        a user to enter information for a new department to be inserted,
        or responds to that submitted form by using OCI to request that an insert
        adding that new department be executed, and then using oci_commit
        to request a commit of that insertion

    by: Sharon Tuttle
    last modified: 2025-04-17

    you can run this using the URL:

    https://nrs-projects.humboldt.edu/~st10/s25cs328/328lect12-1/insert-dept-1.php
-->

<head>
    <title> Insert Department 1 </title>
    <meta charset="utf-8" />

    <?php
        // enable error reporting for now

        ini_set('display_errors', 1);
        error_reporting(E_ALL);

        // get PHP functions called in the body

        require_once("hum_conn_no_login.php");
        require_once("make_dept_form.php");
    ?>
    
    <link href="https://nrs-projects.humboldt.edu/~st10/styles/normalize.css"
          type="text/css" rel="stylesheet" />

    <link href="insert-dept.css" type="text/css" rel="stylesheet" />    
</head>

<body>
    <?php

    // either generate a form to enter a new department's information,
    //     or respond by trying to insert the submitted form's requested
    //     new department

    if ($_SERVER["REQUEST_METHOD"] == "GET")
    {
        ?>
        <h1> Add a New Department </h1>
        <?php
        make_dept_form();
    }

    else
    {
        ?>
        <h1> New Department Status </h1>
        <?php
        // warily responding to form that has been submitted

        // trying to sanitize and grab the submitted new department
        //     info

        $new_dept_num = htmlspecialchars(strip_tags($_POST["deptnum"]));
        $new_dept_name = htmlspecialchars(strip_tags($_POST["deptname"]));
        $new_dept_loc = htmlspecialchars(strip_tags($_POST["deptloc"]));
        
        $conn = hum_conn_no_login();

        // if reach here, connected successfully

        $insert_dept_str = "insert into dept
                            values
                            (:new_dept_num, :new_dept_name, :new_dept_loc)";

        $insert_stmt = oci_parse($conn, $insert_dept_str);

        oci_bind_by_name($insert_stmt, ":new_dept_num", $new_dept_num);
        oci_bind_by_name($insert_stmt, ":new_dept_name", $new_dept_name);
        oci_bind_by_name($insert_stmt, ":new_dept_loc", $new_dept_loc);

        // for a NON-select SQL statement (e.g., insert, update, delete),
        //    oci_execute returns true if successful and false if not

        $result_status = oci_execute($insert_stmt, OCI_DEFAULT);

        if ($result_status == false)
        {
            ?>
            <p> SORRY, could not add new department:
                <?= $new_dept_name ?> </p>
            <?php
        }
        else
        {
            // else, I feel ready to commit!!

            oci_commit($conn);

            ?>
            <p> Added new department:
                <?= $new_dept_name ?> </p>          
            <?php
        }

        // close statement and connection

        oci_free_statement($insert_stmt);
        oci_close($conn);

        // and add a useful link if want to add another department
        ?>
        <p> <a href="<?= htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES) ?>">
            Insert another department </a> </p>
        <?php
    }
    require_once("328footer-plus-end.html");
    ?>