Advertisement
Guest User

Untitled

a guest
Nov 9th, 2011
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.67 KB | None | 0 0
  1. <html>
  2. <head>
  3.   <title>My Bills</title>
  4. </head>
  5.  
  6. <body style="font-family: Arial, Helvetica, sans-serif; color: blue;">
  7.  
  8. <h1>My Bills</h1>
  9.  
  10. <form method=post>
  11.  
  12. <table>
  13.  
  14. <tr>
  15. <th>Item</th>
  16. <th>Amount</th>
  17. </tr>
  18.  
  19. <?php
  20. //Connect
  21.  
  22.  
  23.  
  24.  require('../DBtest.php');
  25.  
  26.  $host = 'localhost';
  27.  $userid = '7admin7';
  28.  $password ='7dosql7';
  29.  
  30.  $db = mysql_perry_pconnect($host, $userid, $password);
  31.  
  32.  if (!$db)
  33.  {
  34.      echo 'Error: Could not connect to database.  Please try again later.';
  35.      exit;
  36.  }
  37.  
  38.  $dbname ='7phpmysql7';
  39.  $dbtest= mysql_perry_select_db($dbname);
  40.  
  41.  
  42.  
  43. //Write records with data if they exist.
  44.  
  45.  
  46. $action = $_POST['action'];
  47.  
  48. if ($action == 'update')
  49. {
  50.     $write_ctr = 1;
  51.    
  52.     // Delete all rows in the table
  53.        
  54.     $query  = "DELETE FROM k820_expenses ";
  55.  
  56.     $result = mysql_query($query);
  57.    
  58.     if (mysql_error()) {
  59.         echo("<br>MySQL Error - Cannot delete from table: ".mysql_error());
  60.         echo("<br>SQL Statement: ".$query);
  61.     }
  62.  
  63.    
  64.     // Loop through table and insert values into the database
  65.    
  66.     while (true)
  67.     {
  68.         $item_name = 'item'."$write_ctr";
  69.         $item_value = $_POST[$item_name];
  70.  
  71.                 $amount = 'amount'."$write_ctr";
  72.         $amount_value = $_POST[$amount];
  73.  
  74.         if (!isset($_POST[$item_name])) {
  75.     break;
  76. }
  77.  
  78.         // Insert an item to the table
  79.  
  80. if (!empty($item_value) && is_numeric($amount_value)) {
  81.         $query = "INSERT INTO k820_expenses
  82.         (item, amount)
  83.         VALUES
  84.         ('$item_value', '$amount_value')";
  85.          
  86.                 $result = mysql_query($query);
  87.        
  88.          if (mysql_error()) {
  89.             echo("<br>MySQL Error - Cannot insert a row into table: ".mysql_error());
  90.             echo("<br>SQL Statement: ".$query);
  91.          }
  92.        
  93.         }
  94.         else {
  95.         if (!empty($item_value) && !is_numeric($amount_value)) {
  96.   $errors[] = "you fucked up $amount_value is not a numeric value";
  97.   }
  98. }
  99.                 $write_ctr++;
  100.  
  101.     }
  102.    
  103. }
  104.  
  105. if (isset($errors)) {
  106.   foreach ($errors as $error) {
  107.     echo "<span style='color:red'>$error<br />\n</span>";
  108.   }
  109. }
  110.  
  111. //Select table and Display
  112.  
  113. $error_count = 0;
  114. $read_ctr = 1;
  115.  
  116. $query  = "SELECT item, amount FROM k820_expenses ";
  117.  
  118. $result = mysql_query($query);
  119.  
  120. if (mysql_error()) {
  121.     echo("<br>MySQL Error- Cannot select from table: ".mysql_error());
  122.     echo("<br>SQL Statement: ".$query);
  123. }
  124.  
  125. if (!empty($result))
  126. {
  127.     $numresults = mysql_num_rows($result);
  128.  
  129.     if ($numresults > 0)
  130.     {
  131.         for ($read_ctr=1; $read_ctr<=$numresults; $read_ctr++)
  132.         {
  133.             $row = mysql_fetch_array($result);
  134.  
  135.  
  136.             $item_value = $row['item'];
  137.             $item_name = 'item'."$read_ctr";
  138.            
  139.             $amount_value = $row['amount'];
  140.             $amount = 'amount'."$read_ctr";
  141.        
  142.             print '<tr>';
  143.             print "<td><input type=text name=$item_name value='$item_value'></td>\n";
  144.             print "<td><input type=text name=$amount value='$amount_value'></td>\n";
  145.            
  146.            
  147.         if (!empty($amount_value))
  148.         {
  149.             if (is_numeric($amount_value))
  150.             {
  151.                 $total = $total + $amount_value;
  152.             } else {
  153.                 print "<td><span style='color:red'>Amount Invalid: $amount_value</span><td>";
  154.                 $error_count++;
  155.             }
  156.         }  
  157.                    
  158.             print '</tr>';
  159.            
  160.         }
  161.     }
  162. }
  163.  
  164.  
  165. //Write formatting lines
  166.  
  167.  
  168.  
  169. for ($i = $read_ctr; $i < $read_ctr + 2; $i++)
  170. {
  171.  
  172.     $item_name =  'item'.$i;
  173.     $amount =  'amount'.$i;
  174.  
  175.     print '<tr>';
  176.     print "<td><input type=text name=$item_name value=''></td>\n";
  177.     print "<td><input type=text name=$amount value=''></td>\n";
  178.     print '</tr>';
  179. }
  180.  
  181.         if ($error_count > 0)
  182.         {
  183.         print "<span style='color:red'><br>Errors: $error_count</span>";
  184.         } else {
  185.         print "<br>Total Bills: $total";
  186.         }
  187.  
  188. ?>
  189.  
  190. </table>
  191.  
  192. <br><input type=submit value=Submit>
  193. <br<br>
  194.  
  195. <!--  Hidden Action Field -->
  196. <input type=hidden name=action value=update>
  197.  
  198. </form>
  199.  
  200. </body>
  201. </html>
  202.  
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement