Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

KikoLdasd's avatar

Query not execute php

nothing is sent to the database, what could I solve for it?

 global $wpdb;
	$order = new WC_Order( $order_id );
	$items = $order->get_items();

	$product_id = 193; // that's a specific product ID
	
	$query = $wpdb->insert('wp_pms_member_subscriptions', array(
	        'id' => 4,
	        'user_id' => 12,
	        'subscribtion_plan_id' => 233,
            'start_date' => null,
	        'expiration_date' => null,
	        'status' => 'active',
	        'paymanet_profile_id' => ' ',
	        'payment_gateway' => ' ',
	        'billing_amount' => ' ',
	        'billing_duration' => ' ',
	        'biling_duration_unit' => ' ',
	        'billing_cycles' => ' ',
	        'billing_next_payment' => null,
	        'billing_last_payment' => null,
	        'trial_end' => null
	    ));
	    
	    
    $wpdb->query($query);

0 likes
9 replies
sr57's avatar

Is your code executed? Set a breakpoint to be sure.

Tray2's avatar

Try adding proper values for the fields. It's most likely that it complains that some value is null and the database doesn't allow it.

KikoLdasd's avatar
function change_role_on_purchase ($order_id ) {

    global $wpdb;
	$order = new WC_Order( $order_id );
	$items = $order->get_items();

	$product_id = 193; // that's a specific product ID

	foreach ( $items as $item ) {

		if( $product_id == $item['product_id'] && $order->user_id ) {
			$user = new WP_User( $order->user_id );
      
      
            $query = $wpdb->insert('wp_pms_member_subscriptions', array(
    	        'user_id' => $user->id,
    	        'subscribtion_plan_id' => 233,
                'start_date' => null,
    	        'expiration_date' => null,
    	        'status' => 'active',
    	        'paymanet_profile_id' => '',
    	        'payment_gateway' => '',
    	        'billing_amount' => '0',
    	        'billing_duration' => '0',
    	        'biling_duration_unit' => '0',
    	        'billing_cycles' => '0',
    	        'billing_next_payment' => null,
    	        'billing_last_payment' => null,
    	        'trial_end' => null
	        ));
            $wpdb->query($query);
		}
	}
}
Tray2's avatar

You still have values that are missing.

 'user_id' => $user->id,
    	        'subscribtion_plan_id' => 233,
                'start_date' => null, //Here
    	        'expiration_date' => null, //Here
    	        'status' => 'active',
    	        'paymanet_profile_id' => '', //Here
    	        'payment_gateway' => '', //Here
    	        'billing_amount' => '0',
    	        'billing_duration' => '0',
    	        'biling_duration_unit' => '0',
    	        'billing_cycles' => '0',
    	        'billing_next_payment' => null, //Here
    	        'billing_last_payment' => null, //Here
    	        'trial_end' => null`// Here

Show us the migration/table structure for the tableyou are trying to insert into.

KikoLdasd's avatar

Look, i tried this query in sql

$wpdb->query = "INSERT INTO `wp_pms_member_subscriptions`(`id`, `user_id`, `subscription_plan_id`, `start_date`, `expiration_date`, `status`, `payment_profile_id`, `payment_gateway`, `billing_amount`, `billing_duration`, `billing_duration_unit`, `billing_cycles`, `billing_next_payment`, `billing_last_payment`, `trial_end`) VALUES (4,12,233,NULL,NULL,'active','','',0,0,0,0,0,null,null)";

he works but but if I run it in the top function, it doesn't work Btw, I use wordpress and wpdb

Tray2's avatar

You have 14 fields in the first query and 15 in the one that works. So you are missing one that is required. In the second example you have 14.

Does the insert function $wpdb->insert() accept arrays as a parameter?

You know tht this is a Laravel forum and not a wordpress forum?

Wordpress related questions is much better placed in a wordpress forum or on stack overflow than here.

Please or to participate in this conversation.