gpt4 book ai didi

php - 动态表单单选按钮搞砸了 foreach 循环 mysql 插入

转载 作者:行者123 更新时间:2023-11-30 22:01:11 25 4
gpt4 key购买 nike

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

我研究了分配隐藏输入并在我的一个单选按钮被选中时禁用它的 js hack,但我发现这有点奇怪。

我的单选按钮的值为 1 attend 或 2 dive,我正在我的脚本中检查该值。我在 [$key+1] 上设置了 +1,但只有在选中所有 radio 时才有效。我很感激任何帮助指出正确的方向,将我的循环设置为仅在检查 radio 时获取数组中的数据。

感谢观看

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

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

最佳答案

我不确定为什么我从来没有见过或听说过这个,直到我在做研究时在帖子中看到它。为了使您的数组与相同的索引对齐,您可以动态或手动分配索引。在我的例子中,我使用了相同的 '.$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

关于php - 动态表单单选按钮搞砸了 foreach 循环 mysql 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43284414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com