PHP方法确定注册的总课程单元是否不超过限制单元
我有一个表格,学生需要注册一个学期的课程,一切正常,但现在面临的问题是如何防止学生注册一个学期超过某个单元 这是我的html代码PHP方法确定注册的总课程单元是否不超过限制单元,php,arrays,Php,Arrays,我有一个表格,学生需要注册一个学期的课程,一切正常,但现在面临的问题是如何防止学生注册一个学期超过某个单元 这是我的html代码 <form id="course" name="coursereg" method="POST" action=""> <table class="table> <thead> <tr> <th>Select Course</th
<form id="course" name="coursereg" method="POST" action="">
<table class="table>
<thead>
<tr>
<th>Select Course</th>
<th>Course Title</th>
<th>Course Code</th>
<th>Course Unit</th>
</tr>
</thead>
<tbody>
<?php
//This is generated from the query not written here
while($rowcourse = mysqli_fetch_assoc($query)){
$courseunit = $rowcourse['courseunit'];
$coursename = $rowcourse['coursename'];
$coursecode = $rowcourse['coursecode'];
?>
<div class="form-group">
<tr>
<td>
<input type="checkbox" name="courses[]" id="course"
value="<?php echo $coursename|$coursecode|$courseunit;?>" width="80px">
</td>
<td>
<input type="text" name="coursename[]" id="course" value="
<?php echo $coursename;?>" style="border:none; background-
color:transparent; width:350px">
</td>
<td>
<input type="text" name="coursecode[]" id="course" value="
<?php echo $coursecode;?>" style="border:none; background-
color:transparent; width:120px">
</td>
<td>
<input type="text" name="courseunit[]" id="course" value="
<?php echo $courseunit;?>" style="border:none; background-
color:transparent; width:80px">
</td>
</tr>
</div>
<?php
}
?>
</tbody>
</table>
</form>
不完全理解这个问题的发展方向,但我猜在进入循环之前,您希望检查$\u POST['courseunit']
的总量
这里有两件事可以计算,行数count($\u POST['courseunit'])
。或者,如果要对数组中的所有值求和。您可以使用array\u sum($\u POST['courseunit'])
这也意味着您可以移动$totalunit>24
,或者这是10?在循环之外,只把东西放在数据库中就可以了
$totalunit=array_sum($_POST['courseunit']);//或者是伯爵?
如果($totalunit>10){
//显示有关数字过高的错误消息。
}否则{
foreach(…){
//放入数据库
}
}
如果您有限制10,那么为什么在这里使用24?此外,这是您的解决方案。如果希望在大于10的情况下不插入数据库,则可以从脚本中简单地exit()
。将下面的代码置于FOR循环之外
// Here change 24 to 10 first
if($totalunit > 10){
echo "<script type='text/javascript'>alert('Courses registered should not be more than 10
Credits');window.location.href='course_registration.php';</script>";
exit(); // Exit function stops the script from executing further.
}
//这里先把24改为10
如果($totalunit>10){
echo“警报('注册的课程不应超过10个
Credits');window.location.href='course_registration.php';
exit();//exit函数停止进一步执行脚本。
}
这就是你的解决方案。还请注意,您不应使用
方法来显示消息。编写脚本的方式不好。我应该纠正你的整个剧本吗?请在评论中告诉我
编辑
根据要求,以下是如何使用简单、干净和优雅的编码编写脚本。请尝试此代码,如果遇到任何困难,请告诉我。另外,一定要记下你的思维和编码方法之间的区别。始终保持代码的简洁和简短
<?php
$msg = null;
if(isset($_POST['submit'])){
if(empty($_POST['courses'])){
$msg = "Error: Select appropriate course(s) you want to register.";
}else{
$checked_arr = $_POST['courses'];
$totalunit = count($checked_arr);
if($totalunit > 10){
$msg = "Courses registered should not be more than 10 Credits";
}else{
foreach($_POST['courses'] as $rows=>$courses){
$coursename = $_POST['coursename'][$rows];
$coursecode = $_POST['coursecode'][$rows];
$courseunit = $_POST['courseunit'][$rows];
$stmt = $conn->prepare("INSERT INTO courses(courseunit, coursename, coursecode)VALUES(:unit, :name, :code)");
$stmt-> bindValue(':unit', $courseunit);
$stmt-> bindValue(':name', $coursename);
$stmt-> bindValue(':code', $coursecode);
$stmt-> execute();
}
if($stmt){
$msg = "Registered successfully!";
}else{
$msg = "Sorry, something went wrong. Please refresh the page and try again.";
}
}
}
}
?>
<!-- Display message this way in HTML part -->
<div class="message"><?php if($msg != null){ echo $msg; } ?></div>
这不是阻止用户添加超过一定数量的内容吗<代码>如果($totalunit>24){
如果是的话,它仍然在做其他的事情,因为它没有用else块包装。它也都在循环中,这很难理解。我认为这样会更好,以便了解更多,因为我还是一个初学者。ThanksI能够通过将它放在for循环之外检查是否满足了条件,但只有一个值插入数据库CA你提交代码的HTML部分吗?我需要看一下。好的,我现在就提交
<?php
$msg = null;
if(isset($_POST['submit'])){
if(empty($_POST['courses'])){
$msg = "Error: Select appropriate course(s) you want to register.";
}else{
$checked_arr = $_POST['courses'];
$totalunit = count($checked_arr);
if($totalunit > 10){
$msg = "Courses registered should not be more than 10 Credits";
}else{
foreach($_POST['courses'] as $rows=>$courses){
$coursename = $_POST['coursename'][$rows];
$coursecode = $_POST['coursecode'][$rows];
$courseunit = $_POST['courseunit'][$rows];
$stmt = $conn->prepare("INSERT INTO courses(courseunit, coursename, coursecode)VALUES(:unit, :name, :code)");
$stmt-> bindValue(':unit', $courseunit);
$stmt-> bindValue(':name', $coursename);
$stmt-> bindValue(':code', $coursecode);
$stmt-> execute();
}
if($stmt){
$msg = "Registered successfully!";
}else{
$msg = "Sorry, something went wrong. Please refresh the page and try again.";
}
}
}
}
?>
<!-- Display message this way in HTML part -->
<div class="message"><?php if($msg != null){ echo $msg; } ?></div>