How to insert values into database if user selects multiple checkboxes PHP
<p>I have to insert roles for user by using check box but for inserting that  i written a code like but it is inserting  value '1' 3 times (if selected 3 check boxes).</p>

<p>my HTML:</p>

<pre><code>&lt;tr&gt;
&lt;td align="right" width="47%" valign="top"&gt;&lt;img
        src="images/required_field.gif" /&gt; User Role:&lt;/td&gt;
&lt;td width="40%" id="anstr"&gt;&lt;input type="checkbox" id="roles_1"
        value="1" name="roles[]" /&gt; &lt;label for="roles_1"&gt;X&lt;/label&gt;&lt;span
        id="levelRole"&gt;&lt;/span&gt;&lt;br /&gt;
    &lt;input type="checkbox" id="roles_2" value="2" name="roles[]" /&gt; &lt;label
        for="roles_2"&gt;Y&lt;/label&gt;&lt;br /&gt;
    &lt;input type="checkbox" id="roles_3" value="3" name="roles[]" /&gt; &lt;label
        for="roles_3"&gt;Z&lt;/label&gt;
&lt;/td&gt;
&lt;/tr&gt;
</code></pre>

<p>my qry:</p>

<pre><code>$roles = $_POST['roles'];
for ($i=0; $i&lt;count($roles); $i++) {
    $check_roles = implode(',', $_POST['roles']);
     $sql1 = $db -&gt;query ( "INSERT INTO user_role_xref( user_id, user_role_id, created_on)
    VALUES ( :p_user_id, :p_user_role_id, :p_created_on)", 
    array (
        'p_user_id' =&gt; 81,
         'p_user_role_id' =&gt; $check_roles,
         'p_created_on' =&gt; date("Y-m-d H:i:s")
         )
    );

}
</code></pre>

<p>Please help me how to do it.</p>
