duanpao4172 2017-04-07 17:58
浏览 67
已采纳

动态表单单选按钮弄乱了foreach循环mysql插入

I have a dynamic form that has all dynamic arrays being set to my php mysql insert script. The records only get inserted correctly if I have all of my radio buttons checked. If I leave any unchecked random records get inserted. It's an attendance table and the inputs below repeat for each person. I'm checking their option with the radio button.

I've looked at the js hack of assigning a hidden input and disabling it if one of my radio buttons get checked but I find that a little weird.

My radio buttons have a value of 1 attend or 2 dive and I'm checking for that value in my script. I have a +1 set on the [$key+1] but that only works if all radios are checked. I appreciate any help pointing me in the correct direction as to setting my loop to only grab the data in the array when the radio is checked.

Thanks for looking

The two offending radio buttons are setup like this with the rest off the variables being set from hidden fields and are all filled with actual values. Everything gets insert if I don't use radio buttons in the form or if they're all checked.

<input  type="radio" name="dive_attend_points['.$row1['_rowid_'].'][dive_attend]" value="'.$dive_points.'">
<input  type="radio" name="dive_attend_points['.$row1['_rowid_'].'][dive_attend]" value="'.$attend_points.'">
        <input type="hidden" name="_rowid_[]" value="' .$row1['_rowid_']. '">
        <input type="hidden" name="location_date[]" value="' .$location_date. '">
        <input type="hidden" name="location_name[]" value="' .$location_name. '">
        <input type="hidden" name="first_name[]" value="' .$row1['first_name']. '">
        <input type="hidden" name="last_name[]" value="' .$row1['last_name']. '">
        <input type="hidden" name="cert_agency[]" value="' .$row1['cert_agency']. '">
        <input type="hidden" name="high_cert[]" value="' .$row1['high_cert']. '">
        <input type="hidden" name="email[]" value="' .$row1['email']. '">
        <input type="hidden" name="phone_number[]" value="' .$row1['phone_number']. '">
        <input type="hidden" name="photo_release[]" value="' .$row1['photo_release']. '">
        <input type="hidden" name="diver_pic[]" value="' .$row1['diver_pic']. '">
        <input type="hidden" name="submitted_email[]" value="' . $submitted_email . '">    
        <input type="hidden" name="food_option[]" value="' .$row2['food_option']. '">

My php loop is setup like this.

foreach ($_POST['location_date'] as $key => $value) {

    $_rowid_ = $_POST['_rowid_'][$key];
    $location_date = $_POST['location_date'][$key];
    $location_name = $_POST['location_name'][$key];
    $first_name = $_POST['first_name'][$key];
    $last_name = $_POST['last_name'][$key];
    $cert_agency = $_POST['cert_agency'][$key];
    $high_cert = $_POST['high_cert'][$key];
    $email = $_POST['email'][$key];
    $phone_number= $_POST['phone_number'][$key];
    $photo_release = $_POST['photo_release'][$key];
    $diver_pic = $_POST['diver_pic'][$key];
    $submitted_email = $_POST['submitted_email'][$key];
    $food_option = $_POST['food_option'][$key];

    foreach ($_POST['dive_attend_points'][$key+1] as $row) {
        $dive_attend = $row['dive_attend'];

        if (!empty($dive_attend)) {
            if($dive_attend == 1) {
                $dive_points = 0;
                $attend_points = 1;
            } elseif($dive_attend == 2) {
                $dive_points = 2;
                $attend_points = 0;
            }
        }
    }

    if($dive_attend == 1 || $dive_attend == 2){
        //mysql insert here.
    }
} 
  • 写回答

1条回答 默认 最新

  • dongqiao6445 2017-04-14 19:51
    关注

    I'm not sure why I've never seen or heard of this until I saw it in a post while doing research. To keep your arrays aligned with the same indexes is you assign an index dynamically or manually. In my case I used the same '.$row1['_rowid_'].' I was using to give my radio button group a unique name so they grouped properly. I placed it inside the empty [] for each input. This technique allowed me to not use those clunky js hacks.

    HTML

        <input  type="radio" name="dive_attend_points['.$row1['_rowid_'].']" value="'.$dive_points.'">
        <input  type="radio" name="dive_attend_points['.$row1['_rowid_'].']" value="'.$attend_points.'">
        <input type="hidden" name="_rowid_['.$row1['_rowid_'].']" value="' .$row1['_rowid_']. '">
        <input type="hidden" name="location_date['.$row1['_rowid_'].']" value="' .$location_date. '">
        <input type="hidden" name="location_name['.$row1['_rowid_'].']" value="' .$location_name. '">
        <input type="hidden" name="first_name['.$row1['_rowid_'].']" value="' .$row1['first_name']. '">
        <input type="hidden" name="last_name['.$row1['_rowid_'].']" value="' .$row1['last_name']. '">
        <input type="hidden" name="cert_agency['.$row1['_rowid_'].']" value="' .$row1['cert_agency']. '">
        <input type="hidden" name="high_cert['.$row1['_rowid_'].']" value="' .$row1['high_cert']. '">
        <input type="hidden" name="email['.$row1['_rowid_'].']" value="' .$row1['email']. '">
        <input type="hidden" name="phone_number['.$row1['_rowid_'].']" value="' .$row1['phone_number']. '">
        <input type="hidden" name="photo_release['.$row1['_rowid_'].']" value="' .$row1['photo_release']. '">
        <input type="hidden" name="diver_pic['.$row1['_rowid_'].']" value="' .$row1['diver_pic']. '">
        <input type="hidden" name="submitted_email['.$row1['_rowid_'].']" value="' . $submitted_email . '">    
        <input type="hidden" name="food_option['.$row1['_rowid_'].']"  value="' .$row2['food_option']. '">
    

    The adjusted php

    foreach ($_POST['location_date'] as $key => $value) {
    
    $_rowid_ = $_POST['_rowid_'][$key];
    $location_date = $_POST['location_date'][$key];
    $location_name = $_POST['location_name'][$key];
    $first_name = $_POST['first_name'][$key];
    $last_name = $_POST['last_name'][$key];
    $cert_agency = $_POST['cert_agency'][$key];
    $high_cert = $_POST['high_cert'][$key];
    $email = $_POST['email'][$key];
    $phone_number= $_POST['phone_number'][$key];
    $photo_release = $_POST['photo_release'][$key];
    $diver_pic = $_POST['diver_pic'][$key];
    $submitted_email = $_POST['submitted_email'][$key];
    $food_option = $_POST['food_option'][$key];
    
    $dive_attend = $_POST['dive_attend_points'][$key]
    
    
    
            if($dive_attend == 1) {
                $dive_points = 0;
                $attend_points = 1;
               } elseif($dive_attend == 2) {
                $dive_points = 2;
                $attend_points = 0;
               }
    
    
    
    
        //mysql insert here.
    
    } //end of for each
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧