Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>My Bills</title>
- </head>
- <body style="font-family: Arial, Helvetica, sans-serif; color: blue;">
- <h1>My Bills</h1>
- <form method=post>
- <table>
- <tr>
- <th>Item</th>
- <th>Amount</th>
- </tr>
- <?php
- //Connect
- require('../DBtest.php');
- $host = 'localhost';
- $userid = '7admin7';
- $password ='7dosql7';
- $db = mysql_perry_pconnect($host, $userid, $password);
- if (!$db)
- {
- echo 'Error: Could not connect to database. Please try again later.';
- exit;
- }
- $dbname ='7phpmysql7';
- $dbtest= mysql_perry_select_db($dbname);
- //Write records with data if they exist.
- $action = $_POST['action'];
- if ($action == 'update')
- {
- $write_ctr = 1;
- // Delete all rows in the table
- $query = "DELETE FROM k820_expenses ";
- $result = mysql_query($query);
- if (mysql_error()) {
- echo("<br>MySQL Error - Cannot delete from table: ".mysql_error());
- echo("<br>SQL Statement: ".$query);
- }
- // Loop through table and insert values into the database
- while (true)
- {
- $item_name = 'item'."$write_ctr";
- $item_value = $_POST[$item_name];
- $amount = 'amount'."$write_ctr";
- $amount_value = $_POST[$amount];
- if (!isset($_POST[$item_name])) {
- break;
- }
- // Insert an item to the table
- if (!empty($item_value) && is_numeric($amount_value)) {
- $query = "INSERT INTO k820_expenses
- (item, amount)
- VALUES
- ('$item_value', '$amount_value')";
- $result = mysql_query($query);
- if (mysql_error()) {
- echo("<br>MySQL Error - Cannot insert a row into table: ".mysql_error());
- echo("<br>SQL Statement: ".$query);
- }
- }
- else {
- if (!empty($item_value) && !is_numeric($amount_value)) {
- $errors[] = "you fucked up $amount_value is not a numeric value";
- }
- }
- $write_ctr++;
- }
- }
- if (isset($errors)) {
- foreach ($errors as $error) {
- echo "<span style='color:red'>$error<br />\n</span>";
- }
- }
- //Select table and Display
- $error_count = 0;
- $read_ctr = 1;
- $query = "SELECT item, amount FROM k820_expenses ";
- $result = mysql_query($query);
- if (mysql_error()) {
- echo("<br>MySQL Error- Cannot select from table: ".mysql_error());
- echo("<br>SQL Statement: ".$query);
- }
- if (!empty($result))
- {
- $numresults = mysql_num_rows($result);
- if ($numresults > 0)
- {
- for ($read_ctr=1; $read_ctr<=$numresults; $read_ctr++)
- {
- $row = mysql_fetch_array($result);
- $item_value = $row['item'];
- $item_name = 'item'."$read_ctr";
- $amount_value = $row['amount'];
- $amount = 'amount'."$read_ctr";
- print '<tr>';
- print "<td><input type=text name=$item_name value='$item_value'></td>\n";
- print "<td><input type=text name=$amount value='$amount_value'></td>\n";
- if (!empty($amount_value))
- {
- if (is_numeric($amount_value))
- {
- $total = $total + $amount_value;
- } else {
- print "<td><span style='color:red'>Amount Invalid: $amount_value</span><td>";
- $error_count++;
- }
- }
- print '</tr>';
- }
- }
- }
- //Write formatting lines
- for ($i = $read_ctr; $i < $read_ctr + 2; $i++)
- {
- $item_name = 'item'.$i;
- $amount = 'amount'.$i;
- print '<tr>';
- print "<td><input type=text name=$item_name value=''></td>\n";
- print "<td><input type=text name=$amount value=''></td>\n";
- print '</tr>';
- }
- if ($error_count > 0)
- {
- print "<span style='color:red'><br>Errors: $error_count</span>";
- } else {
- print "<br>Total Bills: $total";
- }
- ?>
- </table>
- <br><input type=submit value=Submit>
- <br<br>
- <!-- Hidden Action Field -->
- <input type=hidden name=action value=update>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement