dsf23223 2013-08-21 09:47
浏览 91
已采纳

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.

  • 写回答

3条回答 默认 最新

  • dtml3340 2013-08-21 10:03
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块