hyphen
hyphen

Reputation: 967

Call a function after another function has completed

Why would it not work to call the get_accounts() function at the end of the delete_account() function?

function get_accounts() {
    require(ROOT_PATH . "inc/database.php");
    try {
        $results = $db->query("SELECT * FROM account");
    } catch (Exception $e) {
        echo ("ERROR: Data could not be retrieved from the database." . $e);
        exit;
    }
    $accounts = $results->fetchall(PDO::FETCH_ASSOC);
    return $accounts;
}



if(isset($_GET['action']) && ($_GET['action'] == 'delete_account')) {


            require("config.php");
            require("database.php");

            $deleteAccount = $_POST['account'];


            try {
                $results = $db->prepare("DELETE FROM account WHERE account_id_PK = ?");
                $results->bindValue(1, $deleteAccount);
                $results->execute();
            } catch(Exception $e) {
                echo "ERROR: Data could not be removed from the database. " . $e;
                exit;
            }
            echo($deleteAccount);
get_accounts();
};

Basically, I want to run the delete_accounts() function and at the end I would like to run the get_accounts() function, which will refresh the list of accounts on the page after the selected account has been deleted. I can't seem to call a function from within another function, no matter what I try.

Upvotes: 2

Views: 5332

Answers (1)

Chris
Chris

Reputation: 5605

Use the finally part of the try catch & remove the 'exit();'

if(isset($_GET['action']) && ($_GET['action'] == 'delete_account')) {

            require("config.php");
            require("database.php");

            $deleteAccount = $_POST['account'];

            try {
                $results = $db->prepare("DELETE FROM account WHERE account_id_PK = ?");
                $results->bindValue(1, $deleteAccount);
                $results->execute();
            } catch(Exception $e) {
                echo "ERROR: Data could not be removed from the database. " . $e;
            }finally{ 
               get_accounts();
            }
            echo($deleteAccount);
}

Upvotes: 2

Related Questions