I am working on a student course registration system. I have successfully been able to write a code that dynamically generates checkboxes with the Course Codes as values fetched from the database.
<?php
//display checkboxes with course codes as values
$sql = "SELECT *FROM admintbl WHERE Level = ?";
if ($stmt = $con->prepare($sql)){
$stmt->bind_param("s", $pLevel);
$pLevel = $_SESSION['level'];
$stmt->execute();
$result = $stmt->get_result();
$num_rows = $result->num_rows;
if ($num_rows>0){
$cbIndex = 0;
while($row = $result->fetch_assoc()){
$c_code = $row['CourseCode'];
$c_title = $row['CourseTitle'];
?>
<label class="chkLabel" style=""> <?php echo $c_code;?> - <?php echo $c_title;?> <input style="" type="checkbox" class="inChk" name="inChk[]" id="inChk" value="<?php echo $c_code;?>"></input></label>
<?php
}
}else{
echo "Oops! No records found";
}
}
?>
Now, when a user checks on courses he wishes to register and clicks on the submit button, I want a check for duplicates (that is if the user had visited before and left the registration half way), then an error message that displays courses found as duplicates. What I have done so far checks for duplicates, but during submit, it still goes ahead to register new courses even after some duplicate courses are found.
Note: A User can select up to 10 courses or more depending on what is available for the user that year or semester. So, the input is coming in an array form because multiple checkboxes are checked. It is these values that will be used to compare with what is already existing in the database, if any.
I'm having problems with checking the array of checked checkboxes from users to ensure there are no duplicates already in the database.
<?php
//process form input on submit
if (isset($_POST['submit'])){
//fetch data from courses available in database
if (isset($_POST['inChk'])){
foreach($_POST['inChk'] as $inID){
$sql = "SELECT *FROM admintbl WHERE CourseCode = ?";
if ($stmt = $con->prepare($sql)) {
$stmt->bind_param("s", $pccode);
$pccode = $inID;
$stmt->execute();
$result = $stmt->get_result();
$num_rows = $result->num_rows;
if ($num_rows>0){
while ($rows = $result->fetch_assoc()){
$tcode = $rows['CourseCode'];
$ttitle = $rows['CourseTitle'];
$tunit = $rows['CreditUnit'];
$tstatus = $rows['Status'];
}
}
}
//Validate Course selection
$sq = "SELECT RegNo, CourseCode FROM cregtbl WHERE RegNo = ? AND CourseCode = ?";
if ($stmt = $con->prepare($sq)){
$stmt->bind_param("ss", $pregno, $pcscode);
$pregno = $_SESSION['regno'];
$pcscode = $inID;
$stmt->execute();
$result = $stmt->get_result();
$num_rows = $result->num_rows;
//check if course code does not exist already in database
if ($num_rows > 0){
while ($row = $result->fetch_assoc()){
$scode = $row['CourseCode'];
if($scode === $inID){
header('location: cos_reg.php?msg=duplicate');
}
}
}
else{
$sql = "INSERT INTO cregtbl(StudentName, Email, RegNo, Level, Department, Semester, Reg_Date, CourseCode, CourseTitle, CreditUnit, Status) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
$stmt = $con->prepare($sql);
$stmt->bind_param("sssssssssss", $pfname, $pemail, $pregno, $plevel, $pdept, $psemester, $pdate, $pcode, $ptitle,$punit,$pstatus);
$pfname = $_SESSION['name'];
$pemail = $_SESSION['email'];
$pregno = $_SESSION['regno'];
$plevel = $_SESSION['level'];
$pdept = $_SESSION['dept'];
$psemester = $_SESSION['semester'];
$pdate = $rdate;
$pcode = $tcode;
$ptitle = $ttitle;
$punit = $tunit;
$pstatus = $tstatus;
$stmt->execute();
if (!$con->error){
header('location: cos_reg.php?msg3=registered');
}
else{
$msg = $con->error;
}
echo $inID . " =>" . $num_rows . "<br>";
}
}
}
// }
}
else{
header('location: cos_reg.php?data=empty');;
}
}
?>
I really need some help in fixing this. Thank you.