jinhuang1102

php_t1

Dec 2nd, 2021
1,064
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. include('lib/db.php');
  3.  
  4. // Hide login.php
  5. if (!isset($_SESSION['username'])) {
  6.     header('Location: login.php');
  7.     exit();
  8. }
  9.  
  10. //Get Username
  11. $query = "SELECT Username FROM USERS";
  12. $UsernameResult = mysqli_query($db, $query);
  13.  
  14. $Username_data  = array();
  15.  
  16. if($UsernameResult != false){
  17.     while ($row = mysqli_fetch_assoc($UsernameResult))
  18.     {
  19.         $Username_data[] = $row;
  20.     }
  21.     $json =  @json_encode($Username_data);
  22.     print "<script>console.log($json);</script>";
  23. }
  24.  
  25. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  26.  
  27. //    $username = mysqli_real_escape_string($db, $_POST['username']);
  28. //    $username = $_SESSION['username'];
  29.     $VIN = mysqli_real_escape_string($db, $_POST['VIN']);
  30.     $customerID = mysqli_real_escape_string($db, $_POST['customerID']);
  31.     $username = mysqli_real_escape_string($db, $_POST['username']);
  32.     $start_date = mysqli_real_escape_string($db, $_POST['start_date']);
  33.     $completion_date = mysqli_real_escape_string($db, $_POST['completion_date']);
  34.     $odometer_reading= mysqli_real_escape_string($db, $_POST['odometer_reading']);
  35.     $labor_charge = mysqli_real_escape_string($db, $_POST['labor_charge']);
  36.     $RepairDescription = mysqli_real_escape_string($db, $_POST['RepairDescription']);
  37.  
  38. //    echo $username;
  39.  
  40. // ----------------Error Checking for each attribute
  41.  
  42.     $customerID_error = false;
  43.     $vin_error = false;
  44.     $start_date_error = false;
  45.  
  46.     if (empty($VIN)) {
  47.         print ("VIN cannot Empty.");
  48.         $vin_error = true;
  49.     }
  50.  
  51.     if (empty($customerID)) {
  52.         print ("Customer ID cannot Empty.");
  53.         $customerID_error = true;
  54.     } else {
  55.         $int_id = (int)$customerID;
  56.         if ($int_id < 1 or $int_id > 200) {
  57.             print("Customer ID must from 1 to 200 included.");
  58.             $customerID_error = true;
  59.         }
  60.     }
  61.  
  62.     if (empty($start_date)) {
  63.         print("Start Date cannot Empyt");
  64.         $start_date_error = true;
  65.     } else {
  66.  
  67.     }
  68.  
  69.     // If there are no errors executed the query.
  70.     if (!$vin_error and !$customerID and !$start_date_error){
  71.  
  72.         $insert_query = "INSERT INTO RepairForm (VIN , customerID , username , start_date , completion_date , odometer_reading ,
  73.      labor_charge , RepairDescription ) " .
  74.             " VALUES ('$VIN', '$customerID', '$username', '$start_date', '$completion_date',
  75.      '$odometer_reading', '$labor_charge', '$RepairDescription') " ;
  76.  
  77.         // Executed the sql query
  78.         $insert_data_result = mysqli_query($db, $insert_query);
  79.         if(!$insert_data_result){
  80.             echo "sql query failure please check it";
  81. //            echo $insert_query;
  82.         }
  83.     }
  84.  
  85.  
  86. //    if(!(empty($username) && empty($VIN) && empty($customerID) && empty($start_date) && empty($odometer_reading)
  87. //        )){
  88. //        // Insert data in DB
  89. //
  90. //        $insert_query = "INSERT INTO RepairForm (VIN , customerID , username , start_date , completion_date , odometer_reading ,
  91. //      labor_charge , RepairDescription ) " .
  92. //            " VALUES ('$VIN', '$customerID', '$username', '$start_date', '$completion_date',
  93. //      '$odometer_reading', '$labor_charge', '$RepairDescription') " ;
  94. ////        echo $insert_data;
  95. //
  96. //        $insert_data_result = mysqli_query($db, $insert_query);
  97.  
  98.  
  99.  
  100.  
  101. //        include('lib/show_queries.php');
  102. //    }
  103. //    if ($insert_data_result == False) {
  104. //        array_push($error_msg, "INSERT sql has ERROR!!!!!\n");
  105. //        //die("Insert Failure");
  106. //    }
  107.  
  108.  
  109. }  //end of if($_POST)
  110. ?>
  111.  
  112.  
  113. <head>
  114.     <style><?php include 'C:\Code\OMSCS\Databases\cs6400-2021-03-Team099\Phase 3\src\www\Style\syle.css'; ?></style>
  115.     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  116.     <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script>
  117.     <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" ></script>
  118. </head>
  119.  
  120. <!DOCTYPE HTML>
  121. <html>  
  122. <body>
  123.  
  124. <form action="repair_vehicle.php" method="post" enctype="multipart/form-data">
  125.  
  126. <?php include('shared/navbar.php'); ?>
  127.  
  128.         <div class="form-row text-light mt-4" style="justify-content: center;" autocomplete="off">  
  129.             <div class="form-group col-md-4">
  130.                 <input name="VIN" id="VIN" type="text" class="form-control" placeholder="VIN">
  131.             </div>
  132.         </div>
  133.         <div class="form-row text-light" style="justify-content: center;" autocomplete="off">
  134.             <div class="form-group col-md-4">
  135.                 <input name="customerID" id="customerID" type="text" class="form-control" placeholder="CustomerID">
  136.             </div>
  137.         </div>
  138.         <div class="form-row text-light" style="justify-content: center;" autocomplete="off">  
  139.             <div class="form-group col-md-4">
  140.                 <select name="username" id="username" type="text" class="form-control">
  141.                     <option value="" selected>Username</option>
  142.                     <?php  foreach ($Username_data as $row){
  143.                     $Username = urlencode($row['Username']);?>
  144.                     <option value="<?php echo $Username ?>"><?php echo $Username ?></option>
  145.                     <?php } ?>
  146.                 </select>
  147.             </div>
  148.         </div>
  149.         <div class="form-row text-light" style="justify-content: center;" autocomplete="off">  
  150.             <div class="form-group col-md-2">
  151.                 <input name="start_date" id="start_date" type="Date" class="form-control" placeholder="Start Date">
  152.             </div>
  153.             <div class="form-group col-md-2">
  154.                 <input name="completion_date" id="completion_date" type="Date" class="form-control" placeholder="Completion Date">
  155.             </div>
  156.         </div>
  157.         <div class="form-row text-light" style="justify-content: center;" autocomplete="off">
  158.             <div class="form-group col-md-4">
  159.                 <input name="odometer_reading" id="odometer_reading" type="number" min="1" class="form-control" placeholder="Odometer Reading">
  160.             </div>
  161.         </div>
  162.         <div class="form-row text-light" style="justify-content: center;" autocomplete="off">
  163.             <div class="form-group col-md-4">
  164.                 <input name="labor_charge" id="labor_charge" type="number" step="0.01" class="form-control" placeholder="Labor Charge">
  165.             </div>
  166.         </div>
  167.         <div class="form-row text-light" style="justify-content: center;" autocomplete="off">
  168.             <div class="form-group col-md-4">
  169.                 <input name="RepairDescription" id="RepairDescription" type="text" class="form-control" placeholder="Repair Description">
  170.             </div>
  171.         </div>
  172.         <div class="form-row text-light" style="justify-content: center;" autocomplete="off">  
  173.                 <button type="submit" class="btn btn-outline-success mx-1" value="Add" name="submit">Add</button>
  174.                 <button type="submit" class="btn btn-outline-danger mx-1" value="Cancel" name="submit">Cancel</button>
  175.         </div>
  176.  
  177. </form>
  178.  
  179. </body>
  180. </body>
  181. </html>
RAW Paste Data