如何将多个已检查项插入mysql表?

时间:2014-04-05 11:36:53

标签: php html checkboxlist

我正在开发一个电子商店项目我在添加新产品时遇到了一些问题。 这涉及三个表:

  • 产品:p_id,名称,价格
  • 属性:a_id,名称(产品属性,如颜色,品牌等)
  • product_attribute:id,p_id,a_id,value,                     (此处的值例如是属性颜色为白色)

添加我的html表单显示的新产品: 1.文本字段的名称和价格 2.一个复选框列表,显示所有属性以及文本字段以输入其值。

但我很困惑如何在数据库中插入此信息。

这是代码:                         

           $qry = "select att_name from attribute";
           $res = mysql_query($qry);
           while($row=mysql_fetch_array($res))
         {
           ?>

                <tr>
               <td width="136" nowrap="nowrap" class="gridLabels1">
               <input type="checkbox" name="chk_group[]" value='$row' /><?php echo $row['att_name'];?> <br /></td> // checkbox list of all attributes
               <td width="740" class="gridValue1">
               <input name="value" type="text" class="inputText" id="value" size="29"/> </td> // text fields to enter value
           </tr>
           <?php
         }
         ?>

1 个答案:

答案 0 :(得分:1)

首先我建议你使用属性的id来复选框的值。

您可以使用implode函数在数据库中插入属性,这些值将以逗号分隔的方式展开。

由于你已经定义了数组而不是像使用implode一样: 假设你的数组值:

$all_atributes=implode(",",$_POST['chk_group']);
$all_atributes//will output selected ids id1,id4,id5 

并使用此$ all_atributes插入数据库。