Strange PHP DIE() behaviour

48 Views Asked by At

I have a database class, which uses a db->query() and $db->close method The query class supports prepared statements, with ? parameters compared against an array ($data) of values.

The table has a TEXT data field which I'm using to store a large text string in, and a column called UPDATED which uses NOW() on the insert query to show when the record was modified.

If I run the script as standard, the TEXT data field fails to update, however the UPDATED column does. If I var_dump() the variable for the text field before and after the $db->query() method, the values are correct based on the $_POST data being recieved

Here's the kicker though

To see the output on-screen, I added DIE after the $db->query method. When I do this, the TEXT data field and UPDATE both update successfully.

public function save(){
    try {
        $this->products = ( !empty( $this->products ) ? servicing_package::packageWriter( servicing_package::packageReader( $this->products ) ) : null );
        var_dump( $this->products );

        $this->job_created_by = ( !empty( $this->job_created_by ) ? $this->job_created_by : session::getTechnicianId() ); 
        $this->job_current_user = ( !empty( $this->job_current_user ) ? $this->job_current_user : ( !empty( $this->job_technician ) ? $this->job_technician : null ) );
        $data = array(
            $this->entity_id,
            $this->customer_id,
            $this->products,
            $this->action,
            $this->job_created_by,
            $this->job_technician,
            $this->job_current_user,
            $this->job_company,
            $this->status,
            $this->location,
            $this->warranty,
            $this->color,
            $this->invoice_quote,
            $this->invoice_total,
            $this->approved_cost,
            $this->approved_technician,
            $this->approved_date,
            $this->completed,
            $this->collected,
            $this->customer_id,
            $this->products,
            $this->action,
            $this->job_created_by,
            $this->job_technician,
            $this->job_current_user,
            $this->job_company,
            $this->status,
            $this->location,
            $this->warranty,
            $this->color,
            $this->invoice_quote,
            $this->invoice_total,
            $this->approved_cost,
            $this->approved_technician,
            $this->approved_date,
            $this->created,
            $this->completed,
            $this->collected,
        );
        $db = new database();
        $db->query("INSERT INTO `". $db->getdbname() ."`.`servicing__job`(`entity_id`, `customer_id`, `products`, `action`, `job_created_by`, 
                `job_technician`, `job_current_user`, `job_company`, 
                `status`, `location`, `warranty`,`color`, `invoice_quote`, 
                `invoice_total`, `approved_cost`, `approved_technician`, 
                `approved_date`, `created`, `updated`, `completed`, 
                `collected`) 
        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW(),NOW(),?,?) 
            ON DUPLICATE KEY UPDATE `entity_id` = LAST_INSERT_ID(`entity_id`), 
                    `customer_id` = ?, `products` = ?, `action` = ?, 
                    `job_created_by` = ?, `job_technician` = ?, 
                    `job_current_user` = ?, `job_company` = ?, `status` = ?, 
                    `location` = ?, `warranty` = ?, `color` = ?, 
                    `invoice_quote` = ?, `invoice_total` = ?, 
                    `approved_cost` = ?, `approved_technician` = ?, 
                    `approved_date` = ?, `created` = ?, `updated` = NOW(), 
                    `completed` = ?, `collected` = ?", $data );

die;
        /* If the job is new, record who created it */
        if( empty( $this->entity_id ) ) {
            $_technicians = servicing_controls::getTechnicians();
            foreach( $_technicians as $key => $value ) {
                $technicians[ $value['technician_id'] ] = $value['display_name'];
            }
            $user = ( isset( $technicians[ session::getTechnicianId() ] ) ? $technicians[ session::getTechnicianId() ] : "Removed User" );
            $note = "Job ". $db->lastid() ." created by user ". $user;
            servicing_note::addNote( $db->lastid(), $note, $visibility = 0 );
        }

        $this->entity_id = $db->lastid();
        $db->close();
        return $this;
    }
    catch(Exception $e) {    
        echo "Message : " . $e->getMessage();
    }
}
1

There are 1 best solutions below

0
Duncan Wardlaw On

I used a custom class to handle the database connection, the problem I had is that when DIE was called in the save routine, all of the records would update successfully, however the actual issue is that the data WAS being updated in all cases, but one of the initial execution routines loaded the data from the database, and unless DIE was called, would save over the top of the altered data as part of another function.