gpt4 book ai didi

php - 复选框、表单提交以及如何在循环中正确使用 array_fill?

转载 作者:行者123 更新时间:2023-11-29 10:15:26 24 4
gpt4 key购买 nike

我有一个表单,用户可以在其中对评分标准进行一些更改。他们还可以选择将这些更改应用到 1 个或多个记录(类(class)部分编号)。这是我的表单的屏幕截图以及下面的代码:

Form Screenshot

当我单击复选框时,我需要使用复选框中的每个部分编号将表单中的数据插入到数据库中。由于复选框数组仅包含 3 个值,因此它无法完成所有节号行的插入。在我的代码中,您会看到我尝试使用 array_fill,但显然我没有正确使用它或在正确的位置使用它。

这是我提交表单之前我的数据库表的正确外观:http://sqlfiddle.com/#!9/015a20/1

这是我提交表单后数据库表的样子: http://sqlfiddle.com/#!9/15e40a/1

这是我对给我带来问题的变量的测试输出($SectionNumber):

5011 is SectionNumber
5013 is SectionNumber
5099 is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
5011 is SectionNumber
5013 is SectionNumber
5099 is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
5011 is SectionNumber
5013 is SectionNumber
5099 is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber
is SectionNumber

我需要/期望输出显示“5011是SectionNumber”重复11次,然后是“5013是SectionNumber”,最后是“5099是SectionNumber”11次。

HTML 表单:

https://jsfiddle.net/keusv75a/

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" name="snum">
<p>
Select the section for which you'd like to display the grading scale below:<br />
<select name="snum">
<?php
$stmt4 = $connection->prepare("SELECT DISTINCT c.SectionNumber FROM Courses c, Assignments a WHERE a.SectionNumber = c.SectionNumber
") or die($connection->error);
$stmt4->execute();
$result4 = $stmt4->get_result();

while ($row4 = $result4->fetch_assoc()):
?>
<option value="<?php echo $row4['SectionNumber']; ?>" <?php
if ($_POST['snum'] == $row4['SectionNumber']) {
echo "selected";
} ?>><?php echo $row4['SectionNumber']; ?></option>
<?php
endwhile; ?>
</select>
<input class="btn btn-sm btn-primary" type="submit" value="Show Me">
</p>
</form>
<form action="admin_grading_scale2.php" method="post">
<p>Which section(s) would you like to apply this grading scale to?<br>
<?php $stmt4 = $connection->prepare("SELECT DISTINCT SectionNumber FROM Courses
") or die($connection->error);
$stmt4->execute();
$result4 = $stmt4->get_result();

while ($row4 = $result4->fetch_assoc()):
?>
<label>
<input type="checkbox" name="SectionNumber[]" value="<?=$row4['SectionNumber'];?>" id="SectionNumber[]"><?=$row4['SectionNumber'];?></label>
<?php endwhile; ?>
</p>
<div class="form-inline">
<table class="table table-striped">
<thead>
<tr class="table-text-center">
<th scope="col"> Letter Grade</th>
<th scope="col">Percent Toward Grade</th>
<th scope="col">Avg Steps/Day</th>
<th scope="col">Average Active Minutes/Week</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$letter = $row['letter'];
$AssignmentID = $row['AssignmentID'];
$percent = $row['percent'];
$avgsteps = $row['avgsteps'];
$avgweeklymin = $row['avgweeklymin'];
$section = $row['section'];
?>
<input name="id[]" type="hidden" value="<?php echo "$id"; ?>"/>
<input name="section[]" type="hidden" value="<?php echo "$section"; ?>"/>
<input name="AssignmentID[]" type="hidden" value="<?php echo "$AssignmentID"; ?>"/>
<tr class="table-text-center">
<td>
<div class="col-sm-10">
<input type="text" class="form-control" id="letter" name="letter[]" aria-describedby="letter" placeholder="Grade" value="<?php echo "$letter"; ?>">
</div>
</td>
<td>
<div class="col-sm-10">
<input type="text" class="form-control" id="percent" name="percent[]" aria-describedby="percent" placeholder="Percent" value="<?php echo "$percent"; ?>">
</div>
</td>
<td>
<div class="col-sm-10">
<input type="text" class="form-control" id="avgsteps" name="avgsteps[]" aria-describedby="points" placeholder="Average Steps" value="<?php echo "$avgsteps"; ?>">
</div>
</td>
<td>
<div class="col-sm-10">
<input type="text" class="form-control" id="avgweeklymin" name="avgweeklymin[]" aria-describedby="avgweeklymin" placeholder="Average Weekly Activity Minutes" value="<?php echo "$avgweeklymin"; ?>">
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<input class="btn btn-lg btn-primary btn-block" type="submit" value="Save Changes">
</form>

PHP 代码:

$size = count( $_POST[ 'id' ] );
$numofsections = count( $_POST[ 'SectionNumber' ] );

$stmt = $connection->prepare( "INSERT INTO GradingScale SET letter=?,percent=?,avgsteps=?,avgweeklymin=?,section=?,AssignmentID=? ON DUPLICATE KEY UPDATE letter=?,percent=?,avgsteps=?,avgweeklymin=?,section=?,AssignmentID=?" );

for($x = 0; $x < $numofsections; $x++):

$SectionNumber = array_fill(0, $size, $_POST['SectionNumber'][$x]);

$i = 0;
while ( $i < $size ) {
// define each variable
$id = filter_var( $_POST[ 'id' ][ $i ], FILTER_SANITIZE_NUMBER_INT );
$letter = filter_var( $_POST[ 'letter' ][ $i ], FILTER_SANITIZE_STRING );
$percent = filter_var( $_POST[ 'percent' ][ $i ], FILTER_SANITIZE_NUMBER_INT );
$avgsteps = filter_var( $_POST[ 'avgsteps' ][ $i ], FILTER_SANITIZE_NUMBER_INT );
$avgweeklymin = filter_var( $_POST[ 'avgweeklymin' ][ $i ], FILTER_SANITIZE_NUMBER_INT );
$AssignmentID = filter_var( $_POST[ 'AssignmentID' ][ $i ], FILTER_SANITIZE_NUMBER_INT );
$SectionNumber = filter_var( $_POST[ 'SectionNumber' ][ $i ], FILTER_SANITIZE_STRING );
echo $SectionNumber . " is SectionNumber<BR>";

$stmt->bind_param( "siiisisiiisi", $letter,$percent, $avgsteps, $avgweeklymin, $SectionNumber, $AssignmentID, $letter,$percent, $avgsteps, $avgweeklymin, $SectionNumber, $AssignmentID);
$stmt->execute();

//$stmt2->bind_param( "iii", $PointsPossible, $SectionNumber, $AssignmentID );
//$stmt2->execute();

++$i;
}
endfor;

数据库架构

CREATE TABLE `GradingScale` (
`id` int(11) NOT NULL,
`letter` enum('A','B','C','D','F') NOT NULL,
`percent` smallint(4) NOT NULL,
`avgsteps` smallint(6) NOT NULL,
`avgweeklymin` smallint(4) NOT NULL,
`section` varchar(8) NOT NULL,
`AssignmentID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Grading Scale';


INSERT INTO `GradingScale` (`id`, `letter`, `percent`, `avgsteps`, `avgweeklymin`, `section`, `AssignmentID`) VALUES
(1, 'A', 100, 10000, 100, '5011', 1),
(2, 'A', 90, 9500, 90, '5011', 1),
(3, 'B', 80, 9000, 80, '5011', 1),
(4, 'C', 70, 8500, 70, '5011', 1),
(5, 'D', 60, 8000, 60, '5011', 1),
(6, 'F', 50, 7500, 50, '5011', 1),
(7, 'F', 40, 7000, 40, '5011', 1),
(8, 'F', 30, 6500, 30, '5011', 1),
(9, 'F', 20, 6000, 20, '5011', 1),
(10, 'F', 10, 5500, 10, '5011', 1),
(11, 'F', 0, 5000, 0, '5011', 1),
(12, 'A', 100, 10000, 100, '5013', 1),
(13, 'A', 90, 9500, 90, '5013', 1),
(14, 'B', 80, 9000, 80, '5013', 1),
(15, 'C', 70, 8500, 70, '5013', 1),
(16, 'D', 60, 8000, 60, '5013', 1),
(17, 'F', 50, 7500, 50, '5013', 1),
(18, 'F', 40, 7000, 40, '5013', 1),
(19, 'F', 30, 6500, 30, '5013', 1),
(20, 'F', 20, 6000, 20, '5013', 1),
(21, 'F', 10, 5500, 10, '5013', 1),
(22, 'F', 0, 5000, 0, '5013', 1),
(23, 'A', 100, 10000, 100, '5099', 1),
(24, 'A', 90, 9500, 90, '5099', 1),
(25, 'B', 80, 9000, 80, '5099', 1),
(26, 'C', 70, 8500, 70, '5099', 1),
(27, 'D', 60, 8000, 60, '5099', 1),
(28, 'F', 50, 7500, 50, '5099', 1),
(29, 'F', 40, 7000, 40, '5099', 1),
(30, 'F', 30, 6500, 30, '5099', 1),
(31, 'F', 20, 6000, 20, '5099', 1),
(32, 'F', 10, 5500, 10, '5099', 1),
(33, 'F', 0, 5000, 0, '5099', 1);

--
-- Indexes for table `GradingScale`
--
ALTER TABLE `GradingScale`
ADD UNIQUE KEY `id` (`id`,`letter`,`percent`);

-- AUTO_INCREMENT for table `GradingScale`
--
ALTER TABLE `GradingScale`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=34;
COMMIT;

任何帮助将不胜感激!!!

蒂姆

最佳答案

此作业:

$SectionNumber = array_fill(0, $size, $_POST['SectionNumber'][$x]);

正在被这个覆盖:

$SectionNumber = filter_var( $_POST[ 'SectionNumber' ][ $i ], FILTER_SANITIZE_STRING );

我认为你的意思是:

$SectionNumbers = array_fill(0, $size, $_POST['SectionNumber'][$x]);
...
$SectionNumber = filter_var( $SectionNumbers[ $i ], FILTER_SANITIZE_STRING );

注意添加 $SectionNumbers 数组来保存值。

关于php - 复选框、表单提交以及如何在循环中正确使用 array_fill?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50184136/

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