php在我的sql数据库中为每个选中的复选框插入多行

Say i have the following form:

<form action="" method="post">
</BR>
<?php
// create two dropdowns with ajax
echo "<font id=\"categoria\"><select>
";
echo "<option value='0'>Select the Firm</option> 
" ;
echo "</select></font>
";
?>
<?php
echo "<font id=\"subcategoria\"><select>
";
echo "<option value='0'>Select the Claims Hub</option> 
" ;
echo "</select></font>
";
?>

<form method="post" align= "right">
<table >

    <tr><td>&nbsp;</td>
    <tr>
         <td style="text-align: left; ">Criminal  </td>
         <td align="left" >
            <input type="checkbox" name="mattertype" value="1" />
         </td>
    </tr>   
    <tr><td>&nbsp;</td>
    <tr>
         <td style="text-align: left; ">Labour  </td>
         <td align="left" >
            <input type="checkbox" name="mattertype" value="1" />
         </td>
    </tr>   
    <tr><td>&nbsp;</td>
    <tr>
         <td style="text-align: left; ">Civil  </td>
         <td align="left" >
            <input type="checkbox" name="mattertype" value="1" />
         </td>
    </tr>   
    <tr><td>&nbsp;</td>
     <tr>
         <td style="text-align: left; ">Active  </td>
         <td align="left" >
            <input type="checkbox" name="active" value="1" />
         </td>
    </tr>   
    <tr><td>&nbsp;</td>
    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" name="submit" value="Add" align= "right" /></td>
    </tr>

</table>

How would i write the php code to insert the following information as a new record for all the checked checkboxes? For example, i have checked Civil, Labour and Active. I now want the first record that must be inserted to read

Firm Area Mattertype Active (my sql table)

value of categoria value of subcategoria Civl 1
value of categoria value of subcategoria Labour 1

If any or all of the civil, labour or criminal checkboxes are ticked it must insert the value of categoria, value of subcatgoria, first ticked checkbox (mattertype) and if active is ticked then a 1 for active .

Then for the second record again value of categoria, value of subcatgoria, second ticked checkbox (mattertype) and if active is ticked then a 1 for active .

And so on. I hope this makes sense.

First change the mattertype checkbox to use an array for the values, and change the values to something unique for each one (1 for criminal, 2 for labour, 3 for civil)

<input type="checkbox" name="mattertype[]" value="1" />
<input type="checkbox" name="mattertype[]" value="2" />
<input type="checkbox" name="mattertype[]" value="3" />

PHP

$categoria = $_POST['categoria'];
$subcategoria = $_POST['subcategoria'];
$mattertypes = $_POST['mattertype'];
$active = $_POST['active'] == 1 ? 1 : 0;

//$matertypes will be an array so loop over it
foreach( $mattertypes as $type ) {
  switch( $type ) {
     case '1':
       $typename = "Criminal";
     break;
     case '2':
       $typename = "Labour";
     break;
     case '3':
       $typename = "Civil";
     break;
  }
  $SQL = "INSERT into mytable (categoria,subcategoria,mattertype,active) VALUES('$categoria','$subcategoria','$typename','$active')";

  //Do the call to database with the query.
}

Of course sanitize your data before putting it into the database.

in html

  <input type="checkbox" name="mattertype[]" value="1" />

in php

//get mattertype which are checked by

$_POST['mattertype'];// this is come as array then use according to your requirement

Make checkbox name as name="mattertype[]" and change values as well.

Then post as $_POST['mattertype'];