Welcome Guest, Not a member yet? Register   Sign In
Baffling, sporadic error message - validation issue?
#1

[eluser]novice32[/eluser]
On occasion, my CI application throws a very strange and baffling error message. My front-end application is Adobe Flex based and passes data to the CI app via HTTP post methods. In the case of this one model method, AddItem, it throws a DB error message.

------log files for one day----
ERROR - 2010-07-02 15:01:41 --> Query error: Column 'ItemClientID' cannot be null
ERROR - 2010-07-02 15:01:51 --> Query error: Column 'ItemClientID' cannot be null
ERROR - 2010-07-02 15:03:12 --> Query error: Column 'ItemClientID' cannot be null
-----------

It's important to note this happens RARELY (once a week at most). The method has been called successfully 2,000+ times, since the launch of the application. Further confusing this is ItemClientID is assigned at login (in the "user" table for each login) and should never be "null". (ItemClientID is essentially, used to identify which client inserted the record).

Am I incorrectly validating ItemClientID ??


Code:
<?php
/***** CONTROLLER CLASS *****/
class Item extends Controller {

    private $ClientID; //each client has a unique id

    function Item() {
        parent::Controller();

        ......
        
        $this->ClientID = $this->input->post('ClientID'); //reads value when HTTP post is performed to controller's AddItem method
        
        if (empty($this->ClientID)) {
            echo json_encode(array('error'=>'ClientID is required'));
            return; //exit
        }
        ....    
    }    
    
    function AddItem() {
        ...
        // read a bunch of HTTP post values and validate.
        .....
        $this->load->model('Item_model', '', TRUE);
        $rowcount = $this->Item_model->AddItem($this->ClientID,$ItemDescription,....);

        echo json_encode(array('rowcount'=>$rowcount));
    }
}

<?php
/***** MODEL METHOD ****/
class Item_model extends Model {

    function Item_model() {
        parent::Model();
    }
    
    function AddItem($ItemClientID,$ItemDescription,...) {

            //array fields are required in the db schema
            $data = array(
                    'ItemClientID' => $ItemClientID ,
                    'ItemDescription' => $ItemDescription ,
                    ....
            );

            ....

            $this->db->insert('item', $data);
            return $this->db->affected_rows();
        }
}
#2

[eluser]xzela[/eluser]
Hi,

the function empty() can be weird sometimes. a string set to "0" is considered empty.

Check out these type comparisons:
http://www.php.net/manual/en/types.comparisons.php

You might be able to get away with this:
Code:
if (!$this->ClientID) { //flip-a-bit,
            echo json_encode(array('error'=>'ClientID is required'));
            return; //exit
        }

This will either solve the issue or completely break your application. Smile
#3

[eluser]novice32[/eluser]
Thanks! I can see where this would most certainly address the issue where ItemClientID is null/empty and errors out appropriately. I guess the bigger issue for me is how did it become null/empty in the first place, since at the time of signing in, the ItemClientID is passed to the Adobe Flex application and stored in memory. I don't think I'll figure this one out.. I have not been able to reproduce it, nor does it happen frequently in frequency.
#4

[eluser]novice32[/eluser]
I intentionally set ClientID to empty string prior to the check to simulate the error (since HTML post passes only empty stings where null), yet I get the error message in the application ('ClientID is required') but I also get database error in my log file.

Shouldn't the below "return" exit the whole transaction and not continue to the AddItem method??? It had to continue onto the AddItem controller method and then onto AddItem model method to throw the database error.

Code:
$this->ClientID = "";
        if (empty($this->ClientID)) {
            echo json_encode(array('error'=>'ClientID is required'));
            return; //exit
        }
#5

[eluser]novice32[/eluser]
I guess “exit();” as opposed to “return;” works. “exit();” terminates the script altogether.
#6

[eluser]WanWizard[/eluser]
Your doing a 'return' from your controller constructor. That will terminate the constructor, but CI will continue with calling the requested method. 'exit' would be a better option if you really want to terminate.




Theme © iAndrew 2016 - Forum software by © MyBB