<html>
<!--
=====================================================================================================
This PHP script takes data from a form, and writes it to multiple tables (parent/child)
Using a parent table (cust_order2), and 2 child tables (cust_order2_flavor, cust_order2_topping)
=====================================================================================================
-->
<head>
<title>Process an HTML form</title>
</head>
<body>
<?php include "../include.php"; ?>              <!-- hyperlink to see the code -->

<?php
        error_reporting(0);

//----- Retrieve form elements ---------------------------------------------

        $firstname  = $_POST['firstname'];              #get HTML form entry fields 
        $lastname   = $_POST['lastname'];                
        $address    = $_POST['address'];
        $flavors    = $_POST['flavor'];                 #select list array
        $toppings   = $_POST['topping'];                #checkboxes array
        $creditCard = $_POST['creditCard'];

        if (!$firstname) {
            print "<font color=red>Please enter First Name</font>";
            exit; 
        }
        if (!$lastname) {
            print "<font color=red>Please enter Last Name</font>";
            exit; 
        }
        if (!$address) {
            print "<font color=red>Please enter your Address</font>";
            exit; 
        }
        if (!$flavors) {
            print "<font color=red>Please choose ice cream flavor(s)</font>";
            exit; 
        }
        if (!$toppings) {
            print "<font color=red>Please select topping(s)</font>";
            exit; 
        }
        if (!$creditCard) {
            print "<font color=red>Please select Credit Card</font>";
            exit; 
        }

//----- Write data into Database ---------------------------------------------

        $host = 'localhost';
        $DBname    = 'demo2';
        $DBuser    = 'demo2';
        $DBpswd    = 'demo2';
 
        $connect = mysqli_connect($host,$DBuser,$DBpswd,$DBname);   #connect to db server
 
        if (! $connect) 
            die('Could not connect: ' . mysqli_connect_error());

        $firstname = htmlentities($firstname);                  #replace < > ' " & characters;
        $lastname  = htmlentities($lastname);                   #with their html entities;
        $address   = htmlentities($address );                   # < > ' "e; &

        $firstname = mysqli_real_escape_string($connect,$firstname);  #escape all ' " \ newline 
        $lastname  = mysqli_real_escape_string($connect,$lastname);   #with another \, making them
        $address   = mysqli_real_escape_string($connect,$address);    # \' \" \\ \newline

        //insert the parent record ---------------------------------------------

        $insert = "INSERT INTO cust_order2
                   (order_id,firstname,lastname,address,creditCard,cust_id) 
                   VALUES(0,'$firstname','$lastname','$address','$creditCard',1)";
        
//print $insert;                                                        #for debugging        

        $result = mysqli_query($connect,$insert);                       #execute the insert                        

        if (! $result) 
            die('Could not execute update cust_order2: ' . mysqli_error($connect));
            
        //get the last insert id  ----------------------------------------------

        $query = "SELECT LAST_INSERT_ID()";                           #get the last insert id
        
        $cursor = mysqli_query($connect,$query);                      #execute the query                        

        if (! $cursor) 
            die('Could not execute select for last_insert_id: ' . mysqli_error($connect));
        
        $row = mysqli_fetch_row($cursor);
        
        $parent_order_id = $row[0];
       
        mysqli_free_result($cursor);
        
        //insert the child records for flavor ----------------------------------

        foreach ($flavors as $flavor)
        {
            $insert = "INSERT INTO cust_order2_flavor
                       (id, flavor, order_id) 
                       VALUES(0, '$flavor', $parent_order_id)";
        
            $result = mysqli_query($connect,$insert);                 #execute the insert                        

            if (! $result) 
                die('Could not execute update cust_order2_flavor: ' . mysqli_error($connect));
        }                

        //insert the child records for topping ---------------------------------

        foreach ($toppings as $topping)
        {
            $insert = "INSERT INTO cust_order2_topping
                       (id, topping, order_id) 
                       VALUES(0, '$topping', $parent_order_id)";
        
            $result = mysqli_query($connect,$insert);                 #execute the insert                        

            if (! $result) 
                die('Could not execute update cust_order2_topping: ' . mysqli_error($connect));
        }                

        mysqli_close($connect);                                  #close connection

        print "<b>Order Processed Successfully!!!</b>";

//=============================================================================

?>

<br><br>
<hr/>
Click <a href=getFromDB2.php>here</a> or  
      <a href=getFromDB3.php>here</a> to see all orders. 

</body>
</html>