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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向