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

<!--
    by: Sharon Tuttle
    last modified: 2026-04-13

    you can run this using the URL:

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

<head>
    <title> Insert Department </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

        // carefully grab the new department info

        $new_dept_num =
            trim(htmlspecialchars(strip_tags($_POST["deptnum"]))); 

        $new_dept_name =
            trim(htmlspecialchars(strip_tags($_POST["deptname"]))); 

        $new_dept_loc =
            trim(htmlspecialchars(strip_tags($_POST["deptloc"])));

        $conn = hum_conn_no_login();

        // create my insert statement's string

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

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

        // bind values to my bind variables

        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);

        // NOW can execute! for a non-select statement,
        //   should return true if the SQL statement succceeded,
        //   and false otherwise

        $insert_success = oci_execute($insert_stmt, OCI_DEFAULT);

        if ($insert_success == false)
        {
            ?>
	    <p> Sorry, could not add new department:
	        <?= $new_dept_name ?> </p>
	    <?php
	}
	else
	{
	    // it worked! commit the change!

            oci_commit($conn);
	    ?>

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

        // free the statemeent, close the 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
    }
    ?>

    <footer>
    <hr />
    <p>
        Validate by pasting .xhtml copy's URL into<br />
	<a href="https://validator.w3.org/nu">
            https://validator.w3.org/nu
        </a>
	or  
        <a href="https://html5.validator.nu/">
            https://html5.validator.nu/
        </a>
    </p>
    </footer>
</body>
</html>