<?php
  if (isset($_GET['source'])) {
        highlight_file($_SERVER['SCRIPT_FILENAME']);
        exit;
  }
?>
<html>
<head>
<title>Changing Database Information</title>
    
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="theme-color" content="#B5C7DD" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="../css/reset.css">
        <link rel="stylesheet" href="../css/mandy.css">
<!--        <link rel="stylesheet" href="../css/form.css">-->
        <link rel="stylesheet" href="../proj2/css/form.css">

<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon">
    
        <!--brown border hover-->
        <style>
    table {color: var(--midtone); font-size: smaller;  position: relative; z-index: 2; /* max-width: 700px;*/ /* margin: 0 0 0 -64px; */ background: rgba(253, 252, 246, 0.88)}
    tr {border-bottom: 1px dashed rgba(137, 112, 82, 0); border-top: 1px dashed rgba(137, 112, 82, 0);}    
    tbody tr:hover 
        {
/*            background-color: rgba(158, 187, 221, 0.15);*/
            color: var(--dark); border-bottom: 1px solid rgba(137, 112, 82, 0.6); border-top: 1px solid rgba(137, 112, 82, 0.6); position: relative; z-index: 3}
            
/*        modified for this page*/
    tr:nth-child(odd), #selected, th {background-color: rgba(153, 149, 144, 0.07)}
/*        body, .stretch {padding:  0;}*/
    
    table a {color: #9bb9de; text-transform: lowercase}
    @media only screen and (max-width: 700px) {html {background-color: var(--light); background-image:none} table {margin: 0 0 0 -24px;}}
        
        pre {position: fixed; top:0; background: var(--light); z-index: 5}
    </style>
    
    <style>input, .space.button:hover, .space.button:active, #selected {width: 3.5em; opacity: 1; border-color: rgba(153, 149, 144, 0.8)} .space.button {border-color: rgba(153, 149, 144, 0.38); opacity: .8} #selected {color: var(--dark)}
        .stretch {position:fixed; bottom: 0; padding: 340px }
        
/*     specific to this page   */
        .ib {display:inline-block}
        table {margin-left: auto; margin-right: auto;}
        th {font-family: var(--display); font-weight: 400; font-size: larger;}
        th:last-of-type  {padding-right: .4em;}
        th:first-of-type {padding-left: .35em;}
        td {padding-left: .3em; padding-right: .3em;}
        tr {padding-left: 1em; padding-right: .7em;}
        input.button {width: 6em}
        input[type="text"] {width: 12em }
/*        body {background-color: none;}*/
        #s_mi label::after {content:" "}
        #s_mi input {width: 2.2em}
        form div, form {display: flex; flex-wrap: wrap; justify-content: flex-start;}
/*        form div input {margin: inherit 1.5em}*/
        form div div {padding: 0 .6em}
        input {text-transform: capitalize}
        #s_st input {text-transform: uppercase; width: 2.8em}
        #s_zi input {width: 4.5em;}
        input[name*="s_phone"] {width: 3.2em}
        input[name*="s_phone_3"] {width: 3.8em}
        #s_ph span {position: relative; bottom: -.53em}
        #s_ph input {margin-left: .3em; margin-right: .3em;}
        #s_do input {width: 3.8em; margin-left: .2em; margin-right: .2em;}        #s_do input:last-of-type {width: 4.4em}
        label {line-height: 1.22em; font-size: 90%}
        input::placeholder {opacity: .7; font-size: 90%}
    </style>
 
    
    
</head>
<body>

<h2 class="pad-top">    
    
<?php   # need this to be able to view source in the below anchor tag
  $urlPath=dirname($_SERVER['SCRIPT_URL']).'/'; 
  $labsPath=$urlPath.'labs/'; 
?>     

    <a href="../index.php">CS 130A</a> → Lab 13
<a href="<?=$urlPath?>index.php?source" target="labs">Source code</a>

</h2>   


<h4>Changing Database Information</h4>
    <h5 class="ib">Part I.</h5><p class="ib">New Student Registration</p>
    <p class="nudge in">PHP page that will change some of the information in the northwoods database. When a new student successfuly registers, they will be added to the commented out table below. Inspect to see the table values before and after updating records.</p>
    
    
        <?php 
        $s_last = $s_first = $s_mi = $s_address = $s_city = $s_state = $s_zip = $s_phone = $s_phone_1 = $s_phone_2 = $s_phone_3 = $s_dob = $s_dob_m = $s_dob_d = $s_dob_y = ""; 
        $nameError = $numberError = $pass1Error = $pass2Error = ""; 
        $uvalid = $evalid = $pvalid = false; 
         
        if ($_SERVER["REQUEST_METHOD"] == "POST") 
        { 
             
            if (empty($_POST["username"])) 
                { 
                $userError = "<span class=\"error\"> 
                Username is required 
                </span>"; 
                } 
            else 
                { 
                $_SESSION["user"] = $user = test_input($_POST["username"]); 
                if (!preg_match("/^[a-zA-Z0-9, -]*$/", $user)) 
                    { # performs regular expression match 
                    $userError = "<span class=\"error\"> 
                    Only letters, numbers, and white space, dashes and apostrophes allowed 
                    </span>"; 
                    } 
                else { 
                    $uvalid = true; 
                    } 
                } 
             
        } 

        if($uvalid == true && $evalid == true && $pvalid == true)  
            { 
            header("Location: second.php"); 
            } 
             
        function test_input($data) 
            { 
            $data = trim($data); # remove any whitespace from beginning and end of string 
            $data = stripcslashes($data); # strip string of backslashes 
            $data = htmlspecialchars($data); # convert code to HTML escaped code 
            return $data; 
            }         
        ?>     

<h3 class="nudge">Student Registration Form</h3> 
<form class="flex-form" method="POST"> 
             
    <div class="name">
        <div id="s_fi"> 
            <label>First <br> Name:</label> 
            <input name="s_first" type="text" maxlength="50" pattern="[A-Za-z' -]*" title="Letters, spaces, hyphens and apostrophes only" value="<?php echo $s_first;?>" required> 
<!--            <?php echo $nameError;?>  -->
        </div>   
            
        <div id="s_mi"> 
            <label>Middle<br> Initial:</label> 
            <input name="s_mi" type="text" maxlength="1" pattern="[A-Za-z]{0,1}" title="Only one letter allowed." value="<?php echo $s_mi;?>"> 
<!--            <?php echo $nameError;?>  -->
        </div>   
            
        <div id="s_la"> 
            <label>Last<br> Name:</label> 
            <input name="s_last" type="text" maxlength="50" pattern="[A-Za-z' -]*" title="Letters, spaces, hyphens and apostrophes only" value="<?php echo $s_last;?>" required> 
<!--            <?php echo $nameError;?>  -->
        </div>  
    </div>
            
             
    <div class="location">
        <div id="s_ad"> 
            <label>Address:</label> 
            <input name="s_address" type="text" maxlength="50" pattern="\d+ [A-Za-z\d' -.]+" title="Numbers followed by spaces, numbers, letters, apostrophes or periods only" value="<?php echo $s_address;?>" required> 
<!--            <?php echo $locError;?>  -->
        </div>   
            
        <div id="s_ci"> 
            <label>City:</label> 
            <input name="s_city" type="text" maxlength="50" pattern="[A-Za-z' -.]+" title="letters, spaces, dashes, apostrophes or periods only" value="<?php echo $s_city;?>" required> 
<!--            <?php echo $locError;?>  -->
        </div>             
        
        <div id="s_st"> 
            <label>State:</label> 
            <input name="s_city" type="text" maxlength="2" pattern="[A-Za-z]{2}" title="2 letters only" value="<?php echo $s_state;?>" required> 
<!--            <?php echo $locError;?>  -->
        </div>     
        
        <div id="s_zi"> 
            <label>Zip:</label>     
            <input name="s_zip" type="text" maxlength="5" pattern="[\d]{5}" title="5 numbers only" value="<?php echo $s_zip;?>" required> 
<!--            <?php echo $locError;?>  -->
        </div>   
        
        
    </div>            
             
    <div id="s_ph">
      
            <label>Phone:</label>     
            <span>(</span> <input name="s_phone_1" type="text" maxlength="3" pattern="[\d]{3}" title="3-digit area code" value="<?php echo $s_phone_1;?>" required> 
<!--            <?php echo $phError;?>  -->
            <span>)</span> 

            <input name="s_phone_2" type="text" maxlength="3" pattern="[\d]{3}" title="3-digit number" value="<?php echo $s_phone_2;?>" required> 
<!--            <?php echo $phError;?>  -->

            <span>–</span>
            <input name="s_phone_3" type="text" maxlength="4" pattern="[\d]{4}" title="4-digit number" value="<?php echo $s_phone_3;?>" required> 
<!--            <?php echo $phError;?>  -->
        
    </div> 
    
    <div id="s_do">
      
            <label>Date <br>of Birth:</label>     
            
            <input name="s_dob_m" type="text" pattern="((0[1-9])|(1[0-2]))" title="2-digit month" placeholder="MM" value="<?php echo $s_dob_m;?>" required> 
<!--            <?php echo $doError;?>  -->
            
            <input name="s_dob_d" type="text" pattern="((0[1-9])|(1\d)|(2\d)|(3[0-1]))" title="2-digit day" placeholder="DD" value="<?php echo $s_dob_d;?>" required> 
<!--            <?php echo $doError;?>  -->

            <input name="s_phone_y" type="number" min="1900" max="2018" title="4-digit year" placeholder="YYYY" value="<?php echo $s_dob_y;?>" required> 
<!--            <?php echo $doError;?>  -->
        
    </div>
            
            
            
            
            <p><span class="error">Required field</span></p> 
            </div> 
                 
             
            <p><input class="button" name="reset" type="reset" value="Reset"> 
            <input class="button" name="submit" type="submit" value="Submit"></p> 
            <br> 
        </form>     
    
    
    
<!--<pre>-->
<?php
//  include_once('/users/kfreedma/secure/dbvars.inc'); // file which has database user/password
# couldn't figure out how to use an .inc file so just listed the login directly

$dbhost = 'twinpeaks.ccsf.edu';
$dbuser = 'mchen75';
$dbpass = 'jan2992.mc';
$database = 'northwoods';    
    
  $DB = new mysqli($dbhost, $dbuser, $dbpass, $database);
  unset($dbhost, $dbuser, $dbpass, $database); // erase these variables so they wont show up in print_r(get_all_vars()) later on.

  if (!$DB) die("<p>Cannot open database</p>");

$query = "select * from student_mchen75 order by s_id";
   
$result = $DB->query($query);   

   if ($result->num_rows > 0)   # if there are rows of data
        {
        # output search query into a table
        echo "<table class=\"pad-top\"> <tr> <th>s_id</th> <th>s_last</th> <th>s_first</th> <th>s_mi</th> <th>s_address</th> <th>s_city</th> <th>s_state</th> <th>s_zip</th> <th>s_phone</th> <th>s_class</th> <th>s_dob</th> <th>s_pin</th> <th>f_id</th> </tr>";
        while($value = $result->fetch_assoc()) 
            {
            # format the phone numbers + hairspace
            //$phone = preg_replace("/^([\d]{3})([\d]{3})([\d]{4})$/", "($1)&#8202;$2-$3", $value["f_phone"]);
            
            echo "<tr> <td>".$value["s_id"]."</td> <td>".$value["s_last"]."</td> <td>".$value["s_first"]."</td> <td>".$value["s_mi"]."</td> <td>".$value["s_address"]."</td> <td>".$value["s_city"]."</td> <td>".$value["s_state"]."</td> <td>".$value["s_zip"]."</td> <td>".$value["s_phone"]."</td> <td>".$value["s_class"]."</td> <td>".$value["s_dob"]."</td> <td>".$value["s_pin"]."</td> <td>".$value["f_id"]."</td> </tr>";
            }
        echo "</table>";
        } 
   else 
        {
        echo "0 results";
        }
   
$DB->close();
   
  
?>
    
<!--   </pre>     -->

<h5 class="ib pad-top">Part II.</h5><p class="ib"><a href="second.php">Link to Part II here</a></p>
   


    
    <p class="hide">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
</p>

    <div class="stretch">&nbsp;</div>
</body>
</html>