Welcome Guest, Not a member yet? Register   Sign In
Fun query help
#1

[eluser]draconus[/eluser]
I am trying to make a basic check register program as a proof of concept to myself to further my skills. Basically, there is a form that submits the newest transaction, and right before the insert the database needs to give me the account balance from the last transaction, then add or subtract the current form post to generate the new balance.

Here is my controller for the form submit:
Code:
function transaction()
    {
        if(isset($_POST['submit']))
        {
            unset($_POST['submit']);
            $_POST['date'] = date("Y-m-d");
            $this->db->select_max('id');
            $query = $this->db->get('transactions');
            $row = $query->row();
            $query = $this->db->get_where('transactions', array('id' => $row->id));
            $row = $query->row();
            $_Post['balance'] = $row->balance + $_Post['credit'] - $_Post['debit'] - $_Post['fees'];
            
            $this->db->insert('transactions', $_POST);
            redirect('budget/');
        }else{
            $this->load->view('transaction');
        }        
    }

it ends up with the balance inserted always being empty. First off, is there a way to wrap these queries into a transaction to save on database requests, also, is there a way to make this function actually work?

I can't seem to figure it out. Any help would be appreciated.

Thanks
#2

[eluser]WanWizard[/eluser]
To work with transactions, you need a database (or table) that is transaction aware. For MySQL installations, that means you have to use InnoDB instead of MyISAM to create your tables.
The database library has methods to support transactions, commits and rollbacks.

As to your query: PHP is case sensitive, so $_POST is not the same as $_Post. So $_POST['balance'] does not exist when you call the insert method.

And to even further your skills: spend some time reading through the manual. Well worth the time. Then you'll learn never to use $_POST, but to use the form_validation library to validate and sanitize your form variables.
#3

[eluser]draconus[/eluser]
I know not to use the post array directly, but I have found that to mock it all up it is easier to leave the validation class and the models behind, then as a last step in the development, implement such features. It makes it so scaffolding, as well as mashing out the initial interactions has one less level of possible failure. Once i get the basics fleshed out, I will implement those features.

Thanks for the advice, as that should get me one step closer to the final product I am working on. Lemme test and I will let you know how it goes.




Theme © iAndrew 2016 - Forum software by © MyBB