title: How to insert values to table that are there only in the JSON object in PHP
body: <p>I've a table which is created with below SQL statement</p>
  
  <pre><code>CREATE TABLE example (
         col1 varchar(20) DEFAULT NULL,
         col2 varchar(20) DEFAULT NULL,
         col3 varchar(20) DEFAULT NULL
  )
  </code></pre>
  
  <p>I want to insert values to this table from a JSON object. JSON object is constructed such way that its key is the column name and value is the value to be inserted.</p>
  
  <p>How do I write a PHP code to only insert the values that are there in the JSON object?</p>
  
  <p>For instance, If I have a JSON object</p>
  
  <pre><code>{
       "col1": "some value",
       "col3": "some value"
  }
  </code></pre>
  
  <p>The insert statement should only insert values to col1 and col3 and the col2 should remain as NULL.</p>
  
  <p>Please help me out to solve this puzzle. PHP with PDO is preferable.</p>
  
  <p><strong>Experiments performed so far</strong></p>
  
  <pre><code>$object = json_decode($jsonObject);
  
  foreach ($object as $col =&gt; $val) {
         $columns .= $col . ",";
         $values .= $val . ",";
  }
  
  $sql = "INSERT INTO example (" .substr($columns, 0, strlen($columns)-2) .
         ") VALUES (" . substr($values, 0, strlen($columns)-2) . ")";
  </code></pre>