Php script POST request pre_process inserts many rows into mysql database and not only one


#1

Hi I have written this script that is triggered with a post request in a pre process workflow.
This script replaces the received payload with a default payload

{“resource”: [{“Time”:“2018-12-21T07:49:23”,“Gas”:“111111”,“Pressure”:“1111”,“Temperature”:“11111”}]}

that it will be insert into mysql database table “T”:

<?php

$options = ;
$options[‘headers’] = ;
$options[‘headers’][‘Content-Type’] = ‘application/json’;
$options[‘parameters’] = ;
$options[‘parameters’][‘api_key’] = ‘MY_API_KEY’;

$api = $platform[‘api’];
$post = $api->post;

// json string
$json = ‘{“resource”: [{“Time”:“2018-12-21T07:49:23”,“Gas”:“111111”,“Pressure”:“1111”,“Temperature”:“11111”}]}’;
// converts json to php array
$newpayload = json_decode($json, true);

// makes POST call
$result = $post(“sensor_sql/_table/t”, $newpayload, $options);
var_dump($result);
?>

But when I send a post request to my api rest dreamfactory server with “Fiddler4” program my rest api insert into mysql database many row with the payload like this one: {“Time”:“2018-12-21T07:49:23”,“Gas”:“111111”,“Pressure”:“1111”,“Temperature”:“11111”}]} and not only one as it should be.
It seems that the script runs many times and not only one when it is called during pre process.
Is something missing in my script ? Why the script inserts many row in mysql database and not only one ?


#2

I think you’re calling the script recursively. You need not do the $post again. Just modify the payload and let the main function after the prescript handle the insert.

The flow is like this :
PreScript (modify variables )> InsertFunction > PostScript