Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Jordan Czarnik
Jordan Czarnik
1,478 Points

PHP to Mysql Database Not taking data

Hello Everyone configuring a opencart site and I need a little help to figure out what im missing and why the data wont upload into the database.

So I have my four files My Controller My Model My Language My View

Please note these are only parts of the files as im sure you will see but they are the only parts that deal with the section im working on.

\\**Controller File**////// public function transaction() { $this->load->language('sale/customer');

    $this->load->model('sale/customer');

    if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->user->hasPermission('modify', 'sale/customer')) {
        $this->model_sale_customer->addTransaction($this->request->get['customer_id'], $this->request->post['description'], $this->request->post['amount']);

        $data['success'] = $this->language->get('text_success');
    } else {
        $data['success'] = '';
    }

    if (($this->request->server['REQUEST_METHOD'] == 'POST') && !$this->user->hasPermission('modify', 'sale/customer')) {
        $data['error_warning'] = $this->language->get('error_permission');
    } else {
        $data['error_warning'] = '';
    }

    $data['text_no_results'] = $this->language->get('text_no_results');
    $data['text_balance'] = $this->language->get('text_balance');

    $data['column_date_added'] = $this->language->get('column_date_added');
    $data['column_given'] = $this->language->get('column_given');
    $data['column_from'] = $this->language->get('column_from');
    $data['column_description'] = $this->language->get('column_description');
    $data['column_amount'] = $this->language->get('column_amount');

    if (isset($this->request->get['page'])) {
        $page = $this->request->get['page'];
    } else {
        $page = 1;
    }

    $data['transactions'] = array();

    $results = $this->model_sale_customer->getTransactions($this->request->get['customer_id'], ($page - 1) * 10, 10);

    foreach ($results as $result) {
        $data['transactions'][] = array(
            'amount'      => $this->currency->format($result['amount'], $this->config->get('config_currency')),
            'given'       => $result['given'],
            'from'        => $result['from'],
            'description' => $result['description'],
            'date_added'  => date($this->language->get('date_format_short'), strtotime($result['date_added']))
        );
    }

\\\****Model File***//////

public function addTransaction($customer_id, $description = '', $amount = '', $given = '', $order_id = 0) { $customer_info = $this->getCustomer($customer_id);

    if ($customer_info) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "customer_transaction SET customer_id = '" . (int)$customer_id . "', order_id = '" . (int)$order_id . "', description = '" . $this->db->escape($description) . "',  given = '" . $this->db->escape($given) . "',  from = '" . (int)$from . "', amount = '" . (float)$amount . "', date_added = NOW()");

\\*View File(tpl File)*///// <?php if ($error_warning) { ?> <div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?> <button type="button" class="close" data-dismiss="alert">Ɨ</button> </div> <?php } ?> <?php if ($success) { ?> <div class="alert alert-success"><i class="fa fa-check-circle"></i> <?php echo $success; ?> <button type="button" class="close" data-dismiss="alert">Ɨ</button> </div> <?php } ?> <div class="table-responsive"> <table class="table table-bordered table-hover"> <thead> <tr> <td class="text-left"><?php echo $column_date_added; ?></td> <td class="text-left"><?php echo $column_given ?></td> <td class="text-left"><?php echo $column_from; ?></td> <td class="text-left"><?php echo $column_description; ?></td> <td class="text-right"><?php echo $column_amount; ?></td> </tr> </thead> <tbody> <?php if ($transactions) { ?> <?php foreach ($transactions as $transaction) { ?> <tr> <td class="text-left"><?php echo $transaction['date_added']; ?></td> <td class="text-left"><?php echo $transaction['given']; ?></td> <td class="text-left"><?php echo $transaction['from']; ?></td> <td class="text-left"><?php echo $transaction['description']; ?></td> <td class="text-right"><?php echo $transaction['amount']; ?></td> </tr> <?php } ?> <tr> <td>Ā </td> <td class="text-right"><b><?php echo $text_balance; ?></b></td> <td class="text-right"><?php echo $balance; ?></td> </tr> <?php } else { ?> <tr> <td class="text-center" colspan="3"><?php echo $text_no_results; ?></td> </tr> <?php } ?> </tbody> </table> </div> <div class="row"> <div class="col-sm-6 text-left"><?php echo $pagination; ?></div> <div class="col-sm-6 text-right"><?php echo $results; ?></div> </div>

\\*MySql Table*////

--

-- Table structure for table oc_customer_transaction

CREATE TABLE oc_customer_transaction ( customer_transaction_id int(11) NOT NULL AUTO_INCREMENT, customer_id int(11) NOT NULL, order_id int(11) NOT NULL, given text NOT NULL, from text NOT NULL, description text NOT NULL, amount decimal(15,4) NOT NULL, date_added datetime NOT NULL, PRIMARY KEY (customer_transaction_id) ) ENGINE=MyISAM AUTO_INCREMENT=39 DEFAULT CHARSET=utf8 AUTO_INCREMENT=39 ;

-- -- Dumping data for table oc_customer_transaction

Can someone see why the input from given and from on the admin side would not be accepted into the database??

Any help appreciated

1 Answer

Hello there,

That's quite a lot of code to look through! Have you got back a specific error message?