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

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

    you can run this using the URL:

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

<head>
    <title> Insert Department Using a Stored Procedure </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

        // carefully grab the new department info

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

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

        $conn = hum_conn_no_login();

        // here is the string for my stored procedure call

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

        $add_dept_stmt = oci_parse($conn, $add_dept_str);

        // bind values to my bind variables

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

        // NOW can execute! for a non-select statement,
        //    should return true if the SQL statement succceeded,
        //    and false otherwise 
        // [IS THIS TRUE for a PROC???? <-- LOOKS like it, when I
        //    tried after class with a too-long new department name!]

        $proc_success = oci_execute($add_dept_stmt, OCI_DEFAULT);

        if ($proc_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($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
    }
    ?>

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