仅在关联复选框选中MySql时插入输入文本数据

时间:2015-12-01 11:34:40

标签: javascript php jquery html mysql

这里是我的HTMl代码实际上我想插入输入文本数据,如果只选中复选框,并且有50多个输入字段,其中包含要插入的唯一数据,如此HTML

<div class="row">
    <form action="" method="post" name="country_form" class="form" role="form">
        <div class="form-group">

            <div class="col-lg-3 col-xs-12 bg-info">
                <div class="squaredOne pull-left">
                    <input type="checkbox" value="" id="Asian" name="check[0]" />
                    <label for="Asian"></label>
                </div>
                <span class="pull-left">Asian</span><span class="clearfix"></span>
                <input class="bg-alert" type="number" id="bales" name="Asian[0]" min="0" placeholder="Asian Bales">
            </div>

            <div class="col-lg-3 col-xs-12 bg-info">
                <div class="squaredOne pull-left">
                    <input type="checkbox" value="" id="BabyGrow" name="check[1]" />
                    <label for="BabyGrow"></label>
                </div>
                <span class="pull-left">Baby Grow</span><span class="clearfix"></span>
                <input class="bg-alert" type="number" id="bales" name="BabyGrow[1]" min="0" placeholder="Baby Grow Bales">
            </div>

            <div class="col-lg-3 col-xs-12 bg-info">
                <div class="squaredOne pull-left">
                    <input type="checkbox" value="" id="Boys_Pants" name="check[2]" />
                    <label for="Boys_Pants"></label>
                </div>
                <span class="pull-left">Boys Pants</span><span class="clearfix"></span>
                <input class="bg-alert" type="number" id="bales" name="Boys_Pants[2]" min="0" placeholder="Boys Pant Bales">
            </div>

            <div class="col-lg-3 col-xs-12 bg-info">
                <div class="squaredOne pull-left">
                    <input type="checkbox" value="" id="Boys_Colour_Shirts" name="check[3]" />
                    <label for="Boys_Colour_Shirts"></label>
                </div>
                <span class="pull-left">Boys Colour Shirts</span><span class="clearfix"></span>
                <input class="bg-alert" type="number" id="bales" name="Boys_Colour_Shirts[3]" min="0" placeholder="Boys Colour Shirt Bales">
            </div>  

         <div class="form-group">
             <input type="button" value="Next" class="btn-block btn btn-lg btn-info"/>
        </div>

      </form>
     <div>
    </div>

同样在我的数据库中,所有输入字段都在单独的列中 像这样 CT_ID |亚洲人| BabyGrow | Baby_Pants | Baby Color Shirts e.t.c(另外60列)

在复选框字段中选中[0]并在输入文本中亚洲[0]数组相同是否正确?我该怎么做

我要插入或更新的PHP代码是:

for ($j = 0; $j < count($_POST['check']); $j++) {
    if (!empty($_POST['check'][$j])) { /* CHECK IF CHECKBOX IS SELECTED */
        $q = "INSERT INTO container (Asian, BabyGrow) VALUES ('$Asian','$BabyGrow')";
        $q = mysql_query($q);
    }
}

但我不确定,并且很难为具有独特输入字段的50多个div框编写html

1 个答案:

答案 0 :(得分:0)

在代码下面添加一个新的品味页面粘贴,并了解您做错了多少事情.1)您没有使用提交按钮提交n发布值.2)您没有正确命名表单中的输入。 3)php代码中的逻辑.4)没有检查页面是否发布

<div class="row">
    <form action="" method="post" name="country_form" class="form" role="form">
        <div class="form-group">

            <div class="col-lg-3 col-xs-12 bg-info">
                <div class="squaredOne pull-left">
                    <input type="checkbox" value="Asian" id="Asian" name="check[0]" />
                    <label for="Asian"></label>
                </div>
                <span class="pull-left">Asian</span><span class="clearfix"></span>
                <input class="bg-alert" type="number" id="bales" name="Asian" min="0" placeholder="Asian Bales">
            </div>

            <div class="col-lg-3 col-xs-12 bg-info">
                <div class="squaredOne pull-left">
                    <input type="checkbox" value="BabyGrow" id="BabyGrow" name="check[1]" />
                    <label for="BabyGrow"></label>
                </div>
                <span class="pull-left">Baby Grow</span><span class="clearfix"></span>
                <input class="bg-alert" type="number" id="bales" name="BabyGrow" min="0" placeholder="Baby Grow Bales">
            </div>

            <div class="col-lg-3 col-xs-12 bg-info">
                <div class="squaredOne pull-left">
                    <input type="checkbox" value="Boys_Pants" id="Boys_Pants" name="check[2]" />
                    <label for="Boys_Pants"></label>
                </div>
                <span class="pull-left">Boys Pants</span><span class="clearfix"></span>
                <input class="bg-alert" type="number" id="bales" name="Boys_Pants" min="0" placeholder="Boys Pant Bales">
            </div>

            <div class="col-lg-3 col-xs-12 bg-info">
                <div class="squaredOne pull-left">
                    <input type="checkbox" value="Boys_Colour_Shirts" id="Boys_Colour_Shirts" name="check[3]" />
                    <label for="Boys_Colour_Shirts"></label>
                </div>
                <span class="pull-left">Boys Colour Shirts</span><span class="clearfix"></span>
                <input class="bg-alert" type="number" id="bales" name="Boys_Colour_Shirts" min="0" placeholder="Boys Colour Shirt Bales">
            </div>  

         <div class="form-group">
             <input type="submit" value="Next" name="submit" class="btn-block btn btn-lg btn-info"/>
        </div>

      </form>
     <div>
    </div>
    <?php
  if(isset($_REQUEST['submit'])){
    echo "<pre>";
        $query = " INSERT INTO container (";
        $cols=$vals="";
        foreach($_REQUEST['check'] as $chkbx){
          if(isset($_REQUEST[$chkbx]) && $_REQUEST[$chkbx]!=""){
             $cols.=$chkbx.", ";
             $vals.="'".$_REQUEST[$chkbx]."', ";

          }  
        } 
       $vals=rtrim($vals,', ');
       $cols=rtrim($cols,', ');
       echo $query."<br/>".$cols." ) Values (<br/>".$vals.")";
  }

?>