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

时间:2017-04-07 17:58:16

标签: php mysql

我有一个动态表单,所有动态数组都设置为我的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.
    }
} 

1 个答案:

答案 0 :(得分:0)

我不确定为什么我从未见过或听说过这个,直到我在做研究时在帖子中看到它。要使阵列与相同的索引保持一致,可以动态或手动分配索引。在我的情况下,我使用了相同的'.$row1['_rowid_'].'我用来给我的单选按钮组一个唯一的名称,以便他们正确分组。我把它放在每个输入的空[]里面。这种技术让我不会使用那些笨重的js黑客。

<强> 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