<!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,
        (THIS time only asking for new department's name and location),
        or responds to that submitted form by using OCI to request that
        a PL/SQL STORED PROCEDURE add_dept be executed to make a primary
        key for the new department and insert that new department, 
        and then using oci_commit to request a commit of that procedure's 
        insertion

    assumes: PL/SQL stored procedure add_dept has been created in your
        Oracle student database
    uses: hum_conn_no_login.php, make_dept_form_2.php, 328footer-plus-end.html,
          insert-dept.css

    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-2.php
-->

<head>
    <title> Insert Department 2 </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_2.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_2();
    }

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

        // grab the entered new dept info, stripping tags
        //    if there are any (because there shouldn't be...)
        //    and making any odd characters display-only

        $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!

        // request that PL/SQL stored procedure add_dept be called

        $add_dept_str =
            "BEGIN add_dept(:new_dept_name, :new_dept_loc); END;";
        $add_dept_stmt = oci_parse($conn, $add_dept_str);

        oci_bind_by_name($add_dept_stmt, ":new_dept_name", $new_dept_name);
        oci_bind_by_name($add_dept_stmt, ":new_dept_loc", $new_dept_loc);

        oci_execute($add_dept_stmt, OCI_DEFAULT);

        // PL/SQL stored procedure add_dept changes the database, but is 
        //     written so it might be able to be used as part of a larger
        //     transaction, so it does not itself commit that insertion;
        // but this PHP's application is now done, so this transaction IS
        //     now considered completed, and so it is requesting a commit
        //     of the database state at this point

        oci_commit($conn);

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

        // close statement and connection

        oci_free_statement($add_dept_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");
    ?>