<?php //script delete transations
include (CHAPTER_PATH.'/'.$chapter.'/'.$path.'/includes/header.php');
// connect to the db
require(CONNECT);
echo "<div id='content'>";
echo '<h1>Delete Transaction</h1>';
// Get a valid transaction ID, through GET or POST or Select:
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // From view_users.php
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form submission.
$id = $_POST['id'];
} else { // No valid ID, Select one from dropdown
//get all transactions to populate dropdown menu
$q = "SELECT
transaction_id AS id,
CONCAT('FROM: ',cf.last_name,', ',cf.first_name,' (',f.type,'/$',f.balance,') ',' TO: ',ct.last_name,', ',ct.first_name,' (',t.type,'/$',t.balance,') AMOUNT: $',trans.amount,' ON: ', DATE_FORMAT(trans.date_entered,'%M %D, %Y (%l:%i %p)')) AS transaction
FROM transactions AS trans
INNER JOIN accounts AS t
ON trans.to_account_id = t.account_id
INNER JOIN accounts as f
ON trans.from_account_id = f.account_id
INNER JOIN customers as ct
ON t.customer_id = ct.customer_id
INNER JOIN customers as cf
ON f.customer_id = cf.customer_id
ORDER BY trans.date_entered DESC";
if($r = mysqli_query($link,$q) ){
echo "<form action='' method='post' ><p><label for='c'>Select Transaction to Delete</label>";
echo "<select id='c' name='id'>";
while($row = mysqli_fetch_assoc($r)) {
echo "<option";
echo (isset($_POST['transaction']) && $_POST['transaction'] == $row['transaction'])? ' selected':'';
echo " value='$row[id]'>$row[transaction]</option>";
}
echo "</select></p><input type='submit' value='Select'/></form>";
} else {
echo "We are experiencing technical difficulties. Try back later.";
}
}
// Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD'] == 'POST' || isset($id)) {
if (isset($_POST['sure'])) {
if($_POST['sure'] == 'Yes') { // Delete the record.
// Make the query:
$q = "DELETE FROM transactions WHERE transaction_id=$id LIMIT 1";
$r = @mysqli_query ($link, $q);
if (mysqli_affected_rows($link) == 1) { // If it ran OK.
// Print a message:
echo '<p>The transaction has been deleted.</p>';
} else { // If the query did not run OK.
echo '<p class="error">The transaction could not be deleted due to a system error.</p>'; // Public message.
echo '<p>' . mysqli_error($link) . '<br />Query: ' . $q . '</p>'; // Debugging message
//check for any transactions that would prevent deletion
$q="SELECT transaction_id as id, transaction_id from transactions WHERE to_account_id=$id OR from_account_id=$id";
if($r=mysqli_query($link,$q)){
$num = mysqli_num_rows($r);
if($num>0){
echo "<p>This account is involved in $num transactions. <br />
In order to delete this account, you must first delete the following transactions: </p>";
results_to_table_mod($r,'<h3>Transactions using account</h3>','id',false,'delete transactions','chapter=10&path=pursue');
}
}
}
} else { // No confirmation of deletion.
echo '<p>The transaction has NOT been deleted.</p>';
}
} else { // Show the form.
// Retrieve the transaction's information:
$q = "SELECT
transaction_id AS id,
CONCAT('FROM: ',cf.last_name,', ',cf.first_name,' (',f.type,'/$',f.balance,') ',' TO: ',ct.last_name,', ',ct.first_name,' (',t.type,'/$',t.balance,') AMOUNT: $',trans.amount,' ON: ', DATE_FORMAT(trans.date_entered,'%M %D, %Y (%l:%i %p)')) AS transaction
FROM transactions AS trans
INNER JOIN accounts AS t
ON trans.to_account_id = t.account_id
INNER JOIN accounts as f
ON trans.from_account_id = f.account_id
INNER JOIN customers as ct
ON t.customer_id = ct.customer_id
INNER JOIN customers as cf
ON f.customer_id = cf.customer_id
WHERE transaction_id=$id";
$r = mysqli_query ($link, $q);
if (mysqli_num_rows($r) == 1) { // Valid transaction ID, show the form.
// Get the transaction's information:
$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);
// Display the record being deleted:
echo "<h3>Transaction</h3> <p>$row[transaction]</p>
<p>Are you sure you want to delete this transaction?</p>";
// Create the form:
echo '<form action="" method="post">
<input type="radio" name="sure" value="Yes" /> Yes
<input type="radio" name="sure" value="No" checked="checked" /> No
<input type="submit" name="submit" value="Confirm" />
<input type="hidden" name="id" value="' . $id . '" />
</form>';
} else { // Not a valid user ID.
echo '<p class="error">This page has been accessed in error.</p>';
}
}
} // End of the main submission conditional.
// disconnect from the db
require(DISCONNECT);
echo "</div>";
include (CHAPTER_PATH.'/'.$chapter.'/'.$path.'/includes/footer.php');
?>