title: How to insert json to mysql database wp / php - numbers, strings and timestamp
body: <p>I hope you can guide me with the following:</p>
  <p>I am sending a http post request in json to wordpress REST Api, like this:</p>
  <pre><code>{&quot;item1&quot;:26.62,&quot;item2&quot;:319,&quot;item3&quot;:4445,&quot;payload&quot;:&quot;0a66013f115d0001088f013c05&quot;, &quot;timestamp&quot;: &quot;2020-06-26 19:34:31&quot;, &quot;device&quot;: &quot;xx&quot;}
  </code></pre>
  <p>Now in php i have the following to store this in the wordpress connected mysql database, in a custom table, like this:</p>
  <pre><code>function handle_post( WP_REST_Request $request ) {
  global $wpdb;
  $item = $request-&gt;get_json_params();$fields = array();
  $values = array();
  foreach($item as $key =&gt; $val) {
      array_push($fields, preg_replace(&quot;/[^A-Za-z0-9]/&quot;, '', $key));
      array_push($values, $wpdb-&gt;prepare('%f', $val));
  }
  $fields = implode(&quot;, &quot;, $fields);
  $values = implode(&quot;, &quot;, $values);
  $query = &quot;INSERT INTO `tablename_xxx` ($fields) VALUES ($values)&quot;;
  $list = $wpdb-&gt;get_results($query);
  
  return $list;}
  add_action( 'rest_api_init', function () {   register_rest_route( 'data/v1', '/005', array(     'methods' =&gt; 'POST',     'callback' =&gt; 'handle_post', ) );} );
  </code></pre>
  <p>I found this code and it works, but this is only converting the numbers / floats. How should I proceed to include the two key value pairs that contain a string (both numbers and letters). And how can I store the Timestamp? Also, these strings and timestamp are always included, the number of 'items' with values may differ.</p>
  <p>Next, is it possible to use one of the pairs, the value of it, to store it in a (custom) table with matching value in the table title? Can I use a variable somehow?</p>
  <p>Thanks for any help!</p>