I am a beginner to php and am designing student information system where students need to enter 9 subject and its corresponding grade based on the number of sittings. Is there any better way to do this apart from what I have been doing?
These are my codes:
<form name="register" method="POST" action="process2.php" > <div class="col-md-12"> <div class="form-group"> <label>Select Number of Sitting(s)</label> <select name="sitting" id="sitting" class="form-control" onchange="showDiv(this)"> <option value="1">1</option> <option value="2">2</option> </select> </div> </div> <div class="output1" id="div1" name="onesitting"> <div class="col-md-6"> <div class="form-group"> <label>School Name</label> <input type="text" name="schoolname1" id="schoolname" class="form-control" placeholder="School Name" required> </div> </div> <div class="col-md-6"> <div class="form-group"> <label>Exam Type</label> <select name="examboard1" id="examboard" class="form-control" required> <option value="">Exam Type</option> <option value="WAEC">WAEC(SSCE)</option> <option value="WAEC">WAEC(GCE)</option> <option value="NECO">NECO(SSCE)</option> <option value="NECO">NECO(GCE)</option> <option value="NABTEB">NABTEB(SSCE)</option> <option value="NABTEB">NABTEB(GCE)</option> <option value="OTHERS">OTHERS</option> </select> </div> </div> <div class="col-md-6"> <div class="form-group"> <label>Exam Date</label> <?php
$examyear = range(1960, 2050);
?> <select class="form-control" name="examdate1" id="examdate" placeholder="Exam Date"> <option value="">Select date</option> <?php
foreach($examyear as $examdate){
echo '<option value= "' .$examdate.'">'.$examdate.'</option>';
}
?> </select> </div> </div> <div class="col-md-6"> <div class="form-group"> <label>Exam Number</label> <input type="text" class="form-control" name="examno1" id="examno" placeholder="Exam Number"> </div> </div> <br> <table width="100%" cellpadding="0" cellspacing="0" border="0" class="table table-borderless" id="example"> <tr> <td width="12%"><label class="control-label">S/NO</label></td> <td width="53%"><label class="control-label">SUBJECTS</label></td> <td width="35%"><label class="control-label">GRADE</label></td> </tr> <tr> <td>1</td> <td> <?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?> <select name="subject11" class="form-control" id="subject"> <option value="" selected="selected">Select subject</option> <?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?> </select> </td> <td> <select name="grade11" class="form-control"> <option value=""> Select</option> <option value="A1">A1</option> <option value="B2">B2</option> <option value="B3">B3</option> <option value="C4">C4</option> <option value="C5">C5</option> <option value="C6">C6</option> <option value="D7">D7</option> <option value="E8">E8</option> <option value="F9">F9</option> </select> </td> </tr> <tr> <td>2</td> <td> <?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?> <select name="subject12" class="form-control" id="subject"> <option value="" selected="selected">Select subject</option> <?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?> </select> </td> <td> <select name="grade12" class="form-control"> <option value=""> Select </option> <option value="A1">A1</option> <option value="B2">B2</option> <option value="B3">B3</option> <option value="C4">C4</option> <option value="C5">C5</option> <option value="C6">C6</option> <option value="D7">D7</option> <option value="E8">E8</option> <option value="F9">F9</option> </select> </td> </tr> <tr> <td>3</td> <td> <?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?> <select name="subject13" class="form-control" id="subject"> <option value="" selected="selected">Select subject</option> <?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?> </select> </td> <td> <select name="grade13" class="form-control"> <option value=""> Select </option> <option value="A1">A1</option> <option value="B2">B2</option> <option value="B3">B3</option> <option value="C4">C4</option> <option value="C5">C5</option> <option value="C6">C6</option> <option value="D7">D7</option> <option value="E8">E8</option> <option value="F9">F9</option> </select> </td> </tr> <tr> <td>4</td> <td> <?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?> <select name="subject14" class="form-control" id="subject"> <option value="" selected="selected">Select subject</option> <?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?> </select> </td> <td> <select name="grade14" class="form-control"> <option value=""> Select </option> <option value="A1">A1</option> <option value="B2">B2</option> <option value="B3">B3</option> <option value="C4">C4</option> <option value="C5">C5</option> <option value="C6">C6</option> <option value="D7">D7</option> <option value="E8">E8</option> <option value="F9">F9</option> </select> </td> </tr> <tr> <td>5</td> <td> <?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?> <select name="subject15" class="form-control" id="subject"> <option value="" selected="selected">Select subject</option> <?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?> </select> </td> <td> <select name="grade15" class="form-control"> <option value="">Select </option> <option value="A1">A1</option> <option value="B2">B2</option> <option value="B3">B3</option> <option value="C4">C4</option> <option value="C5">C5</option> <option value="C6">C6</option> <option value="D7">D7</option> <option value="E8">E8</option> <option value="F9">F9</option> </select> </td> </tr> </table> <br> </div> <div class="form2a" id="div2a" name="twositting"> <br> <h4 style="text-align:center; margin-top: 0px; margin-bottom: 5%; background-color:#0d4115; width: auto; color: white;"> SECOND SITTING</h4> <div class="col-md-6"> <div class="form-group"> <label>School Name</label> <input type="text" name="schoolname2" id="schoolname" class="form-control" placeholder="School Name" required> <span id="error_schoolname" class="text-danger"></span> </div> </div> <div class="col-md-6"> <div class="form-group"> <label>Exam Type</label> <select name="examboard2" id="examboard" class="form-control" required> <option value="">Exam Type</option> <option value="WAEC">WAEC(SSCE)</option> <option value="WAEC">WAEC(GCE)</option> <option value="NECO">NECO(SSCE)</option> <option value="NECO">NECO(GCE)</option> <option value="NABTEB">NABTEB(SSCE)</option> <option value="NABTEB">NABTEB(GCE)</option> <option value="OTHERS">OTHERS</option> <span id="error_examboard" class="text-danger"></span> </select> </div> </div> <div class="col-md-6"> <div class="form-group"> <label>Exam Date</label> <?php
$examyear = range(1960, 2099);
?> <select class="form-control" name="examdate2" id="examdate" placeholder="Exam Date"> <option value="">Select date</option> <?php
foreach($examyear as $examdate){
echo '<option value= "' .$examdate.'">'.$examdate.'</option>';
}
?> <span id="error_examdate" class="text-danger"></span> </select> </div> </div> <div class="col-md-6"> <div class="form-group"> <label>Exam Number</label> <input type="text" class="form-control" name="examno2" id="examno" placeholder="Exam Number"> <span id="error_examno" class="text-danger"></span> </div> </div> <br> <table width="100%" cellpadding="0" cellspacing="0" border="0" class="table table-borderless" id="example"> <tr> <td width="12%"><label class="control-label">S/NO</label></td> <td width="53%"><label class="control-label">SUBJECTS</label></td> <td width="35%"><label class="control-label">GRADE</label></td> </tr> <tr> <td>1</td> <td> <?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?> <select name="subject21" class="form-control" id="subject"> <option value="" selected="selected">Select subject</option> <?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?> </select> </td> <td> <select name="grade21" class="form-control"> <option value=""> Select</option> <option value="A1">A1</option> <option value="B2">B2</option> <option value="B3">B3</option> <option value="C4">C4</option> <option value="C5">C5</option> <option value="C6">C6</option> <option value="D7">D7</option> <option value="E8">E8</option> <option value="F9">F9</option> </select> </td> </tr> <tr> <td>2</td> <td> <?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?> <select name="subject22" class="form-control" id="subject"> <option value="" selected="selected">Select subject</option> <?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?> </select> </td> <td> <select name="grade22" class="form-control"> <option value=""> Select </option> <option value="A1">A1</option> <option value="B2">B2</option> <option value="B3">B3</option> <option value="C4">C4</option> <option value="C5">C5</option> <option value="C6">C6</option> <option value="D7">D7</option> <option value="E8">E8</option> <option value="F9">F9</option> </select> </td> </tr> <tr> <td>3</td> <td> <?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?> <select name="subject23" class="form-control" id="subject"> <option value="" selected="selected">Select subject</option> <?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?> </select> </td> <td> <select name="grade23" class="form-control"> <option value=""> Select </option> <option value="A1">A1</option> <option value="B2">B2</option> <option value="B3">B3</option> <option value="C4">C4</option> <option value="C5">C5</option> <option value="C6">C6</option> <option value="D7">D7</option> <option value="E8">E8</option> <option value="F9">F9</option> </select> </td> </tr> <tr> <td>4</td> <td> <?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?> <select name="subject24" class="form-control" id="subject"> <option value="" selected="selected">Select subject</option> <?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?> </select> </td> <td> <select name="grade24" class="form-control"> <option value=""> Select </option> <option value="A1">A1</option> <option value="B2">B2</option> <option value="B3">B3</option> <option value="C4">C4</option> <option value="C5">C5</option> <option value="C6">C6</option> <option value="D7">D7</option> <option value="E8">E8</option> <option value="F9">F9</option> </select> </td> </tr> <tr> <td>5</td> <td> <?php include("config.php");
$sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
?> <select name="subject25" class="form-control" id="subject"> <option value="" selected="selected">Select subject</option> <?php
if($count > 0){
while($row = mysqli_fetch_assoc($query)){
$subject_id = $row['subject_id'];
$subject_name = $row['subject_name'];
echo "<option>" . $row['subject_name'] . "</option>";
}
}else{
echo "<option value = ''>Subject not available</option>";
}
?> </select> </td> <td> <select name="grade25" class="form-control"> <option value="">Select </option> <option value="A1">A1</option> <option value="B2">B2</option> <option value="B3">B3</option> <option value="C4">C4</option> <option value="C5">C5</option> <option value="C6">C6</option> <option value="D7">D7</option> <option value="E8">E8</option> <option value="F9">F9</option> </select> </td> </tr> </table> </div> <button type="submit" class="btn btn-primary" name="submit">Submit</button> </form>
The above codes are my html codes which consists of two forms and it displays based on the number of sittings selected. I use javascript to display the form based on the number of sittings.
These are my php codes to insert the selected input into the database.
$sitting = mysqli_real_escape_string($conn, $_POST['sitting']);
$schoolname1 = mysqli_real_escape_string($conn, $_POST['schoolname1']);
$examboard1 = mysqli_real_escape_string($conn, $_POST['examboard1']);
$examdate1 = mysqli_real_escape_string($conn, $_POST['examdate1']);
$examno1 = mysqli_real_escape_string($conn, $_POST['examno1']);
$subject11 = mysqli_real_escape_string($conn, $_POST['subject11']);
$grade11 = mysqli_real_escape_string($conn, $_POST['grade11']);
$subject12 = mysqli_real_escape_string($conn, $_POST['subject12']);
$grade12 = mysqli_real_escape_string($conn, $_POST['grade12']);
$subject13 = mysqli_real_escape_string($conn, $_POST['subject13']);
$grade13 = mysqli_real_escape_string($conn, $_POST['grade13']);
$subject14 = mysqli_real_escape_string($conn, $_POST['subject14']);
$grade14 = mysqli_real_escape_string($conn, $_POST['grade14']);
$subject15 = mysqli_real_escape_string($conn, $_POST['subject15']);
$grade15 = mysqli_real_escape_string($conn, $_POST['grade15']);
$schoolname2 = mysqli_real_escape_string($conn, $_POST['schoolname2']);
$examboard2 = mysqli_real_escape_string($conn, $_POST['examboard2']);
$examdate2 = mysqli_real_escape_string($conn, $_POST['examdate2']);
$examno2 = mysqli_real_escape_string($conn, $_POST['examno2']);
$subject21 = mysqli_real_escape_string($conn, $_POST['subject21']);
$grade21 = mysqli_real_escape_string($conn, $_POST['grade21']);
$subject22 = mysqli_real_escape_string($conn, $_POST['subject22']);
$grade22 = mysqli_real_escape_string($conn, $_POST['grade22']);
$subject23 = mysqli_real_escape_string($conn, $_POST['subject23']);
$grade23 = mysqli_real_escape_string($conn, $_POST['grade23']);
$subject24 = mysqli_real_escape_string($conn, $_POST['subject24']);
$grade24 = mysqli_real_escape_string($conn, $_POST['grade24']);
$subject25 = mysqli_real_escape_string($conn, $_POST['subject25']);
$grade25 = mysqli_real_escape_string($conn, $_POST['grade25']);
$enter = "INSERT INTO applicants(sitting, schoolname1, examboard1,
examdate1, examno1, subject11, grade11, subject12, grade12, subject13,
grade13, subject14, grade14, subject15, grade15, schoolname2, examboard2,
examdate2, examno2, subject21, grade21, subject22, grade22, subject23,
grade23, subject24, grade24, subject25, grade25) VALUES('$sitting',
'$schoolname1', '$examboard1', '$examdate1', '$examno1', '$subject11',
'$grade11', '$subject12', '$grade12', '$subject13', '$grade13',
'$subject14', '$grade14', '$subject15', '$grade15','$schoolname2',
'$examboard2', '$examdate2', '$examno2', '$subject21', '$grade21',
'$subject22', '$grade22', '$subject23', '$grade23', '$subject24',
'$grade24', '$subject25', '$grade25')";
$result = mysqli_query($conn, $enter);
if($result){
echo "<script type='text/javascript'>alert('Details Uploaded !!');
</script>";
header('location: success.php');
}else{
echo "<script type='text/javascript'>alert('Details Failed !!');</script>";
}
}
Please is there any better way to do this apart from the one I did above?