Advertisement
Guest User

Untitled

a guest
Mar 7th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <!--
  3. To change this license header, choose License Headers in Project Properties.
  4. To change this template file, choose Tools | Templates
  5. and open the template in the editor.
  6. -->
  7. <html>
  8. <head>
  9. <meta charset="UTF-8">
  10. <title></title>
  11. </head>
  12. <body>
  13. <?php
  14.  
  15. //function for connecting to the database
  16. function Connect(){
  17. $dbhost = '127.0.0.1';
  18. $dbuser = 'root';
  19. $dbpass = '';
  20. $db = 'dbvehiculos';
  21.  
  22. $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
  23.  
  24. if (!$conn) {
  25. die("Fallo en conexión: " . mysqli_error());
  26. }
  27.  
  28. $db_select = mysqli_select_db($conn, $db);
  29.  
  30. if (!$db_select) {
  31. die("Fallo en conexión a base de datos: " . mysqli_error());
  32. }
  33. return $conn;
  34. }
  35.  
  36. //searches the database for the id number input
  37. if (isset($_POST['check'])) {
  38. $conn = Connect();
  39.  
  40. $ID = $_POST['p_ID'];
  41.  
  42. $sql = "SELECT * FROM `test` WHERE id=$ID";
  43.  
  44. $qry = mysqli_query($conn, $sql) or die('Consulta fallida');
  45.  
  46. if (!$qry) {
  47. die(mysqli_error());
  48.  
  49. }
  50. $result = mysqli_query($conn, $sql);
  51. while ($row = mysqli_fetch_object($result)) {
  52. echo 'Amount Owed per month: ' . $row->monthlyAmount;
  53. echo '<br>';
  54. echo 'Months past due: ' . $row->monthsOwed;
  55. echo '<br>';
  56. echo 'Interest rate per month: ' . $row->intRate;
  57. echo '<br>';
  58. echo 'Daily interest: ' . $row->intRate * $row->monthsOwed / 30;
  59. echo '<br>';
  60. echo 'Rent due: ' . $row->monthlyAmount * $row->monthsOwed;
  61. echo '<br>';
  62. echo 'Interest charge: ' . (Calculate($row->monthlyAmount, $row->intRate / 100, $row->monthsOwed) - ($row->monthlyAmount * $row->monthsOwed)); //calculates the interest amount with the Calculate function
  63. echo '<br>';
  64. echo 'Total: ' . (Calculate($row->monthlyAmount, $row->intRate / 100, $row->monthsOwed)); //calculates the total amount using the Calculate function
  65. echo '<br>';
  66. echo '<br>';
  67. }
  68. mysqli_close($conn);
  69. ?>
  70. <form method = "post" action = "<?php $_PHP_SELF; ?>">
  71. <table width = "400" border = "0" cellspacing = "1" cellpadding = "2">
  72. <tr>
  73. <td width = "100" >Account ID</td>
  74. <td><input name = "p_ID" type = "text" value="<?php echo $ID; ?>" id = "post_ID">
  75. </td>
  76. </tr>
  77. <tr>
  78. <td width = "100" >NUMBER OF MONTHS TO REMOVE</td>
  79. <td><input name = "p_Remove" type = "text" id = "post_Remove"></td>
  80. </tr>
  81. <tr>
  82. <td width = "100"> </td>
  83. <td>
  84. <input name = "recheck" type = "submit" id = "recheck" value = "RECHECK">
  85. </td>
  86. </tr>
  87. </table>
  88. </form>
  89.  
  90. <?php
  91.  
  92. }
  93. //removes the entered months from the database and recalculates the amount
  94. else if (isset($_POST['recheck'])) {
  95. $conn = Connect();
  96.  
  97. $ID = $_POST['p_ID'];
  98. $remove = $_POST['p_Remove'];
  99.  
  100. $sqlRemove = "UPDATE `test` SET monthsOwed = monthsOwed - " . $remove . " WHERE id=$ID";
  101. $qryRemove = mysqli_query($conn, $sqlRemove) or die('Consulta fallida');
  102.  
  103. if (!$qryRemove) {
  104. die(mysqli_error());
  105.  
  106. }
  107. $sql = "SELECT * FROM `test` WHERE id=$ID";
  108.  
  109. $qry = mysqli_query($conn, $sql) or die('Consulta fallida');
  110.  
  111. if (!$qry) {
  112. die(mysqli_error());
  113.  
  114. }
  115. $result = mysqli_query($conn, $sql);
  116. while ($row = mysqli_fetch_object($result)) {
  117. echo '<br>';
  118. echo '<br>';
  119. echo 'UPDATED AND REMOVED MONTHS';
  120. echo '<br>';
  121. echo 'RECALCULATED';
  122. echo '<br>';
  123. echo '<br>';
  124. echo 'Amount Owed per month: ' . $row->monthlyAmount;
  125. echo '<br>';
  126. echo 'Months past due: ' . $row->monthsOwed;
  127. echo '<br>';
  128. echo 'Interest rate per month: ' . $row->intRate;
  129. echo '<br>';
  130. echo 'Daily interest: ' . $row->intRate * $row->monthsOwed / 30;
  131. echo '<br>';
  132. echo 'Rent due: ' . $row->monthlyAmount * $row->monthsOwed;
  133. echo '<br>';
  134. echo 'Interest charge: ' . (Calculate($row->monthlyAmount, $row->intRate / 100, $row->monthsOwed) - ($row->monthlyAmount * $row->monthsOwed)); //calculates the interest amount with the Calculate function
  135. echo '<br>';
  136. echo 'Total: ' . (Calculate($row->monthlyAmount, $row->intRate / 100, $row->monthsOwed)); //calculates the total amount using the Calculate function
  137. echo '<br>';
  138. echo '<br>';
  139. }
  140. mysqli_close($conn);
  141. } else {
  142.  
  143. ?>
  144. <form method = "post" action = "<?php $_PHP_SELF; ?>">
  145. <table width = "400" border = "0" cellspacing = "1" cellpadding = "2">
  146. <tr>
  147. <td width = "100">ID NUMBER</td>
  148. <td><input name = "p_ID" type = "text" id = "post_ID"></td>
  149. </tr>
  150. <tr>
  151. <td width = "100"> </td>
  152. <td> <input name = "check" type = "submit" id = "check" value = "CHECK">
  153. </td>
  154. </tr>
  155. </table>
  156. </form>
  157.  
  158. <?php
  159. }
  160. //function calculates the interest per month
  161. function Calculate($monthly, $rate, $months)
  162. {
  163. for ($i = 1; $i <= $months; $i++) {
  164. //first if determins the first month amount
  165. if ($i == 1) {
  166. $amount = ($monthly * ($rate)) + $monthly;
  167. }
  168. //takes the first month and calculates it into the other months
  169. else if ($i <= $months) {
  170. $amount = ($amount + $monthly) + (($amount + $monthly) * ($rate));
  171. }
  172. }
  173. return $amount;
  174. }
  175. ?>
  176.  
  177. </body>
  178. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement