title: PDO insert statement with loop through $_POST array
body: <p>How can I loop through $_POST array where $_POST form names are equal to database columns names, and do it safely from MySQL injections?</p>
  
  <p><strong>I will have form with about 40 inputs.</strong></p>
  
  <p>For example, I have html form (<strong>index.php</strong>):</p>
  
  <pre><code>&lt;form action="form.php" id="myForm" method="POST" enctype="multipart/form-data"&gt;
  
  User: &lt;input type="text" name="owner_name"&gt;&lt;br&gt;
  User-email: &lt;input type="text" name="owner_email"&gt;&lt;br&gt;
  User-id: &lt;input type="text" name="owner_id"&gt;&lt;br&gt;
  Pictures: &lt;br&gt;&lt;input name="pictures[]" type="file" size="50" maxlength="100000" multiple&gt;&lt;br&gt;
  &lt;input type="submit" value="Send" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And I have <strong>form.php</strong> file:</p>
  
  <pre><code>$pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  
  // Check connection
  if ($pdo-&gt;connect_error) {
      die("Connection failed: " . $pdo-&gt;connect_error);
  }
  
  $statement = $pdo-&gt;prepare("INSERT INTO claims_motor(owner_email, owner_name, owner_id)
      VALUES(:owner_email, :owner_name, :owner_id)");
  $statement-&gt;execute(array(
      "owner_email" =&gt; $_POST['owner_email'],
      "owner_name" =&gt; $_POST['owner_name'],
      "owner_id" =&gt; $_POST['owner_id']
  ));
  </code></pre>
  
  <p>How can I build a foreach loop for $_POST, so I don't need to write this code for 40 $_POST variables?</p>