Dynamically insert values
<p>I'd like to loop through form fields and dynamically take form field data and insert it into the database. I don't want to have to bind a value for each form field, but rather loop through the values in an array. It doesn't seem to be working the way I have it. Any suggestions?</p>

<p>Thanks</p>

<pre><code>&lt;?php
    $FormFields = array();
    $Values = array();
    global $i;
    $i = 0;

    $conn= new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
    foreach($_POST as $key =&gt; $value) {
        $FormFields[++$i] = $key;
        $Values[$i] = $value;

        echo $FormFields[$i] . "&lt;br /&gt;";
        echo $Values[$i] . "&lt;br /&gt;";

        //loop through all form fields and bind value
        $sql = "INSERT INTO sectiona ($FormFields[$i]) VALUES (:$i)";
        $st = $conn-&gt;prepare($sql);
        $st-&gt;bindValue(":$i", $Values[$i]);
        $st-&gt;execute();

    }
    echo "Total Fields: " . $i;
    $conn = null;

?&gt; 
</code></pre>

<p>EDIT:</p>

<p>Why does it create a new row for each data entry?
Shouldn't it just be one row?</p>

<p><img src="http://i.stack.imgur.com/uIN7B.jpg" alt="my image"></p>
