Change Column Value with PHP Variable

Hi,

Just wondering if anyone can help here. I have been banging my head on the keyboard for hours over this and starting to change my mind about using Monday.com for our offices.

What I am trying to do is update a column value within the table of a worksheet using the value I have stored in a PHP variable, but it doesn’t work at all. Does anyone know what I am doing wrong?

Code below…

$token = [MYTOKEN];
$tempUrl = "https://api.monday.com/v2/";

$office_id = 212; //THIS VALUE IS ACTUALLY COMING FROM MYSQL DATABASE QUERY

$headers = ['Content-Type: application/json', 'User-Agent: [MYTEAM] GraphQL Client', 'Authorization: ' . $token];

$query = 'mutation ($columnVal: Int!) { change_column_value (board_id: [BOARD_ID], item_id: [ITEM_ID], column_id: [COLUMN_ID], value: $columnVal) { id } }';

$vars = $office_id;

$data = @file_get_contents($tempUrl, false, stream_context_create([
 'http' => [
 'method' => 'POST',
 'header' => $headers,
 'content' => json_encode(['query' => $query, 'variables' => $vars]),
 ]
]));

it looks like you’re using the change_simple_column_value syntax with the change_column_value mutation.
See this :

Also i think that the part 'variables' => $vars where $vars = $office_id; is wrong because 'variables' should be an array.

Try with this :

'variables' => [
    'columnVal' => 'my new column value'
]

Thanks for your response on this. Much appreciated. Unfortunately it is still not working though. I am getting this error:

Variable columnVal of type JSON! was provided invalid value

And the code I am using is shown below:

$token = [MYTOKEN];
$tempUrl = "https://api.monday.com/v2/";

$office_id = 212; //THIS VALUE IS ACTUALLY COMING FROM MYSQL DATABASE QUERY

$headers = ['Content-Type: application/json', 'User-Agent: [MYTEAM] GraphQL Client', 'Authorization: ' . $token];

$query = 'mutation ($columnVal: JSON!) { change_column_value (board_id: [BOARD_ID], item_id: [ITEM_ID], column_id: [COLUMN_ID], value: $columnVal) { id } }';

$vars = ['variables' => [
    'columnVal' => $office_id
]];

$data = @file_get_contents($tempUrl, false, stream_context_create([
 'http' => [
 'method' => 'POST',
 'header' => $headers,
 'content' => json_encode(['query' => $query, 'variables' => $vars]),
 ]
]));

First, you specified in the mutation that $columnVal is of type JSON but in the variables array you actually don’t send a JSON but an Int.
Maybe try to change this with $query = 'mutation ($columnVal: Int!) ...

Also, the variables array in the content part of the request is one level too deep.
Because of this :

$vars = [
    'columnVal' => $office_id
];

and this :

'content' => json_encode(['query' => $query, 'variables' => $vars])

the actual object being encoded with json_encode looks like this :

[
    'query' => $query,
    'variables' => [
        'variables' => [
            'columnVal' => $office_id
        ]
    ]
]

but it should looks like this :

[
    'query' => $query,
    'variables' => [
        'columnVal' => $office_id
    ]
]

Last thing is the name of mutation you are using : change_column_value.
It is a little more complexe to use since the actual data required with this query is an object that could change the value of the column, its colour, its label, etc…

Maybe try with a simpler mutation like change_simple_column_value which works with columnId and columnValue only.

Complete rewrite would look like this :

$token = [MYTOKEN];
$tempUrl = "https://api.monday.com/v2/";

$office_id = 212; //THIS VALUE IS ACTUALLY COMING FROM MYSQL DATABASE QUERY

$headers = ['Content-Type: application/json', 'User-Agent: [MYTEAM] GraphQL Client', 'Authorization: ' . $token];

$query = 'mutation ($columnVal: Int!) { change_simple_column_value (board_id: [BOARD_ID], item_id: [ITEM_ID], column_id: [COLUMN_ID], value: $columnVal) { id } }';

$vars = [
    'columnVal' => $office_id
];

$data = @file_get_contents($tempUrl, false, stream_context_create([
 'http' => [
 'method' => 'POST',
 'header' => $headers,
 'content' => json_encode(['query' => $query, 'variables' => $vars]),
 ]
]));

Hope this will help :slight_smile:

Thanks again :slight_smile: I am getting an entirely new error now from using your exact code. Here it is:

Type mismatch on variable $columnVal and argument value (Int! / String!)

What do you make of this? I don’t see any mismatch here at all. I am passing an integer value through as 212 so shouldn’t be an issue.

Since your $office_id value is actually coming from MySQL database query, maybe it’s a string instead of a int.

Maybe with this :

mutation ($columnVal: String!) ...

Complete script should look like this :

$token = [MYTOKEN];
$tempUrl = "https://api.monday.com/v2/";

$office_id = 212; //THIS VALUE IS ACTUALLY COMING FROM MYSQL DATABASE QUERY

$headers = ['Content-Type: application/json', 'User-Agent: [MYTEAM] GraphQL Client', 'Authorization: ' . $token];

$query = 'mutation ($columnVal: String!) { change_simple_column_value (board_id: [BOARD_ID], item_id: [ITEM_ID], column_id: [COLUMN_ID], value: $columnVal) { id } }';

$vars = [
    'columnVal' => $office_id
];

$data = @file_get_contents($tempUrl, false, stream_context_create([
 'http' => [
 'method' => 'POST',
 'header' => $headers,
 'content' => json_encode(['query' => $query, 'variables' => $vars]),
 ]
]));

I thought exactly the same but it is definitely an integer. I even put an integer value in there just to test and it never worked.

I also tried String! but I get a similar error as before (shown below):

Type mismatch on variable $columnVal and argument value (String! / JSON!)

Hey @mcmaster1986 - just to clarify, what column type are you trying to send this numeric value to?

Hey @dsilva . The column types are setup as Numbers.

Is anyone able to help with this?

Can you please add a var_dump of $office_id somewhere in your code and paste the result here so we can confirm the type of this data?

Also, can you confirm that you are using the change_simple_column_value mutation and not the change_column_value.

The error message tells me that $office_id is a string but Monday is expecting a JSON.

Type mismatch on variable $columnVal and argument value (String! / JSON!)

Hey @mcmaster1986 - as @ellis pointed out above, monday is expecting a JSON format when using the change_column_value mutation.

Would you mind posting a snippet of the $office_id result on here as suggested above?

-Daniel

@ellis @dsilva – Yes absolutely. Please find below the var_dump of $office_id…

int(212)

I have managed to fix this now :slight_smile: Thanks for your help guys. Totally appreciate it.

The issue was resolved by sending the $office_id in a JSON format and updating the mutation so the Int! was changed to JSON!

Amazing. Thanks again. Have a great weekend.

2 Likes

@mcmaster1986 Amazing!! Glad this is working for you.

If you run into anything else please let us know :slight_smile:

-Daniel

Good news :slight_smile:

1 Like