dongxiane0395 2015-12-01 11:34
浏览 53

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

here is my HTMl code actually i want to insert the input text data if only the checkbox is checked and there are 50+ input fields with unique data to insert like this 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>

Also in my Database all the input fields are in separate column like this CT_ID | Asian | BabyGrow | Baby_Pants | Baby Colour Shirts e.t.c (60 more columns)

In checkbox field Check[0] and in Input text Asian[0] array is same is that correct? How can i do this

My PHP code idea to insert or update is:

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);
    }
}

but i am not sure and also it's hard to write html for 50+ div boxes with unique input fields

  • 写回答

1条回答 默认 最新

  • dongxia8656 2015-12-01 12:52
    关注

    take a new taste page paste below code and have an idea how many things you did wrong.1) you have not used submit button to submit n post the values.2)you have not properly named the inputs in the form. 3)logic in php code.4) have not checked whether page posted or not

    <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.")";
      }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比