【问题标题】:Dynamic form radio buttons mess up foreach loop mysql insert动态表单单选按钮搞砸了foreach循环mysql插入
【发布时间】:2017-09-03 05:01:59
【问题描述】:

我有一个动态表单,其中所有动态数组都设置为我的 php mysql 插入脚本。只有当我检查了所有单选按钮时,记录才会正确插入。如果我留下任何未经检查的随机记录,就会被插入。 这是一个考勤表,下面的输入为每个人重复。我正在使用单选按钮检查他们的选项。

我已经查看了分配隐藏输入并在我的一个单选按钮被选中时禁用它的 js hack,但我觉得这有点奇怪。

我的单选按钮的值为 1 参加或 2 潜水,我正在脚本中检查该值。我在 [$key+1] 上设置了 +1,但只有在检查所有收音机时才有效。我很感激任何帮助指出我正确的方向,将我的循环设置为仅在检查无线电时抓取数组中的数据。

感谢收看

两个有问题的单选按钮是这样设置的,其余的都是从隐藏字段中设置的变量,并且都填充了实际值。如果我不使用表单中的单选按钮或者它们都被选中,那么所有内容都会被插入。

<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']. '">

我的 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];

    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.
    }
} 

【问题讨论】:

  • 我认为您可以通过提供实际的 HTML 输出而不是用于制作它的 PHP 来使这个问题更容易回答,并且还可以提供更多的 HTML 行。例如,“location_date”是如何计算在内的?
  • 我添加了用于循环的其余输入设置变量。他们全部隐藏并且不受用户控制。我让他们设置的唯一字段是单选按钮。这是一个列出用户的动态 html 表,因此这些用户每行都会重复。

标签: php mysql


【解决方案1】:

我不知道为什么我从来没有见过或听说过这个,直到我在做研究时在帖子中看到它。为了使您的数组与相同的索引对齐,您可以动态或手动分配索引。在我的例子中,我使用了相同的 '.$row1['_rowid_'].' 来给我的单选按钮组一个唯一的名称,以便它们正确分组。我将它放在每个输入的空 [] 中。这种技术让我不用使用那些笨拙的 js hack。

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']. '">

调整后的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

【讨论】:

    猜你喜欢
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2014-10-07
    相关资源
    最近更新 更多