osheadavid7
osheadavid7

Reputation: 1

php mysqli insert works on dev but not in production, insert id 0 on auto increment column

I have the following function php function that submits an insert request into a mysql database. On the testing/dev server it works each and every time but when cloning (the entire application via git) to the production machine, it returns no error but fails to insert a row into the database. All tables in the databases (dev and production) are created using the same scripts (the actual data values stored may be different due to testing etc).

After prepare, bind and execute I check the status of $sql_str, which should be either true or false. In each case it returns true and inserts a new row on the dev server. On the production server it still returns true but no row is inserted. autocommit is on by default. Is there some cases where execute will return for an INSERT where no row is inserted?

function submit_new_batch_sheet_request($bulk_liquid_code, 

$daycode, $creation_date,
$db,$log,$session){

$sql_str=$db->prepare("INSERT INTO batch_sheets (batch_sheet_name, BULK_LIQUID, DAYCODE, DATE, OPERATOR_BREW) VALUES (?,?,?,?,?)");
$data = [];

if(!$sql_str){
    $log->lwrite($session['first_name'].' '.$session['last_name'].' New Batch Sheet submission Entry: Error 1: '.$db->error);
    $data = array('Error:',-1);
}
else{
    $batch_sheet_name = get_next_batch_sheet_name($db,$log,$session);
    $insertdate = date('Y-m-d', strtotime($creation_date));

    $log->lwrite($session['first_name'].' '.$session['last_name'].' New Batch Sheet submission: Sheet Name: '.$batch_sheet_name);
    $log->lwrite($session['first_name'].' '.$session['last_name'].' New Batch Sheet submission: Insert Date: '.$insertdate);


    $sql_str->bind_param('ssssd',$batch_sheet_name, $bulk_liquid_code,
                         $daycode,
                         $insertdate, 
                         $session['PersonID']);

    if(!$sql_str){
        $log->lwrite($session['first_name'].' '.$session['last_name'].' New Batch Sheet submission: Error 2: '.$sql_str->error);
        $log->lwrite($session['first_name'].' '.$session['last_name'].' New Batch Sheet submission: Error 2: '.$db->error);
        $data = array('Error:',-2);
    } 
    else{
        $sql_str->execute();


        if(!$sql_str){ 
            $error_str=$db->error;
            $log->lwrite($session['first_name'].' '.$session['last_name'].' New Batch Sheet submission: Error 3: '.$error_str);
            $data = ['Error:',-3];
        }
        else{
            $batch_sheet_id=$sql_str->insert_id;
            $log->lwrite($session['first_name'].' '.$session['last_name'].' New Batch Sheet submission returned: '.$batch_sheet_id);
            $data = array('Success:',$batch_sheet_id);
        }

    }

}

return json_encode($data);

}

EDIT1:

Logs from submission attempt. Note: Table has auto_increment column so insert_id should be non-zero.

[06/Sep/2017:16:17:31] (submit_new_batch_sheet) USER NAME New Batch Sheet submission: Sheet Name: 1709.7

[06/Sep/2017:16:17:31] (submit_new_batch_sheet) USER NAME New Batch Sheet submission: Insert Date: 2017-09-05

[06/Sep/2017:16:17:31] (submit_new_batch_sheet) USER NAME New Batch Sheet submission returned: 0

EDIT 2:

From the comments I see my execute check should be:

if($sql_str->execute()){...}

upon making the change I now get an error stating the insert failed.

EDIT:3

I have now solved the issue. The issue with the php function was I had the incorrect condition in my 'if' statements.

if($sql_str->execute()){...}

The above line now correctly shows the execute where as

if($sql_str)

was incorrect (thanks to the commenter for pointing this out). The true error was related to a restriction on one of the columns on the production server (did not accept null values). This column now been modified to accept NULL values and the function submits.

Upvotes: 0

Views: 89

Answers (1)

osheadavid7
osheadavid7

Reputation: 1

As mentioned in the comments my required return status comes from

if($sql_str->execute()){...}

not

if($sql_str){...}

having made this change the function correctly reports that the execute failed. The error log (from db->error) stated that a column was passed a null value that did not allow null values. This is a separate issue and thanks to the commenters I now consider this question solved. The issue with the column rejecting NULL values is a separate matter.

Upvotes: 0

Related Questions