1

I am trying to update a row in a custom database table by $wpdb->update();. In my opinion my code is right but for some reason my database row doesn't update.

Can anyone help me with this?

I want to update a the status of a transaction in the table wp_mollie_transactions where the transaction_id is equal to the $order_id, which I received from from my payment provider.

(Off course I made $wpdb global in this file)

// Update database
    $wpdb->update( $wpdb->prefix . 'mollie_transactions',
        array(
            'status'    => $payment->status
        ),
        array( 
            'transaction_id'    => $order_id
        )
    );
  • Have you checked that $order_id and $payment->status are valid variables, and there is definitely an entry in the database with a matching transaction_id? Make sure debugging is enabled and see if wpdb throws any errors. – TheDeadMedic May 2 '14 at 9:25
3

Before trying to write to the database, always validate the values you're putting in:

$fValid = true;
if ( !isset( $payment->status ) ) {
    echo 'Error: Payment status is not set';
    $fValid = false;
}
if ( !isset( $order_id )  ) {
    echo 'Error: Order ID is not set';
    $fValid = false;
}

if ( $fValid ) {
    // Update database
    $fSuccess = $wpdb->update( $wpdb->prefix . 'mollie_transactions',
        array(
            'status'            => $payment->status
        ),
        array( 
            'transaction_id'    => $order_id
        )
    );

    echo sprintf( 'Update %s where Payment Status was %s and Transaction ID was %s', $fSuccess? 'Succeeded':'Failed', $payment->status, $order_id );
}

Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.