When I send data to the database from the form, it does not receive both values in subject_id and mark field
this is source code
<?php include_once 'header.php'; ?>
<?php include_once 'sidebar.php'; ?>
<?php include_once 'navtop.php'; ?>
<?php include_once 'class.token.php'; ?>
<div class="container-fluid">
<div class="row">
<div class="col-lg-4">
<div class="card shadow mb-4 text-right">
<div class="card-body">
<?php
if (isset($_POST['insert'])) {
if(Token::check($_POST['token']) AND !empty($_POST['token'])){
$student_id=@output($_POST['student_id']);
$marks_percentage=@output($_POST['marks_percentage']);
$class_id=@output($_POST['class_id']);
$date_created=@output($_POST['date_created']);
//
if(!empty($student_id) ){
$stm = $db->prepare("SELECT * FROM results WHERE student_id=:student_id AND class_id=:class_id");
$stm->bindParam(":student_id", $student_id, PDO::PARAM_STR);
$stm->bindParam(":class_id", $class_id, PDO::PARAM_STR);
$stm->execute();
$rowCount = $stm->rowCount();
if($rowCount == 0){
$stm = $db->prepare("INSERT INTO results (id, student_id, marks_percentage, class_id, date_created) VALUES (NULL, :student_id, :marks_percentage, :class_id, :date_created);");
$stm->bindParam(":student_id", $student_id, PDO::PARAM_STR);
$stm->bindParam(":marks_percentage", $marks_percentage, PDO::PARAM_STR);
$stm->bindParam(":class_id", $class_id, PDO::PARAM_STR);
$stm->bindParam(":date_created", $date_created, PDO::PARAM_STR);
if(!$stm->execute()){ echo "errr1";}
if (!empty($stm)) {
$result_id=@output($_POST['result_id']);
$subject_id=@output($_POST['subject_id']);
$mark=@output($_POST['mark']);
$result_id = $db->lastInsertId();
//$id = $db->insert_id;
$stm2 = $db->prepare("INSERT INTO result_items (id,result_id,subject_id,mark) VALUES (NULL,:result_id,:subject_id,:mark);");
//$stm2->bindParam(":id", $id, PDO::PARAM_STR);
$stm2->bindParam(":result_id", $result_id, PDO::PARAM_STR);
$stm2->bindParam(":subject_id", $subject_id, PDO::PARAM_STR);
$stm2->bindParam(":mark", $mark, PDO::PARAM_STR);
//$stm2->execute();
if(!$stm2->execute()){ echo "errrr2"; }
}
$db = null;
//direct("./department_list.php");
echo "success";
}else {
echo "This name is taken";
}
}else {
echo "Try again.";
}
}else {
echo "error";
}
}
?>
<form method="post" action="" id="">
<div id="msg" class=""></div>
<div class="form-group">
<label for="" class="control-label">Student</label>
<select name="student_id" id="student_id" class="form-control select4 select2-sm">
<option></option>
<?php
$classes = $conn->query("SELECT s.*,concat(c.level,'-',c.section) as class,concat(firstname,' ',middlename,' ',lastname) as name FROM students s inner join classes c on c.id = s.class_id order by concat(firstname,' ',middlename,' ',lastname) asc ");
while($row = $classes->fetch_array()):
?>
<option value="<?php echo $row['id'] ?>" data-class_id='<?php echo $row['class_id'] ?>' data-class='<?php echo $row['class'] ?>' <?php echo isset($student_id) && $student_id == $row['id'] ? "selected" : '' ?>><?php echo $row['student_code'].' | '.ucwords($row['name']) ?></option>
<?php endwhile; ?>
</select>
<small id="class"><?php echo isset($class) ? "Current Class: ".$class : "" ?></small>
<input type="hidden" name="class_id" value="<?php echo isset($class_id) ? $class_id: '' ?>">
</div>
<hr class="mt-4">
<div class="form-group">
<label for="" class="control-label">Subject:</label>
<select name="subject_id" id="subject_id" class="form-control select5">
<option></option>
<?php
$classes = $conn->query("SELECT * FROM subjects order by subject asc ");
while($row = $classes->fetch_array()):
?>
<option value="<?php echo $row['id'] ?>" data-json='<?php echo json_encode($row) ?>'><?php echo $row['subject_code'].' | '.ucwords($row['subject']) ?></option>
<?php endwhile; ?>
</select>
</div>
<div class="form-group">
<label>mark:</label>
<input type="text" name="mark" id="mark" class="form-control form-control-user" placeholder="First Name">
</div>
<p align="center"> <button class="btn btn-block btn-primary" type="button" id="add_mark"><i class="fas fa-fw fa-plus"></i></button></p>
</div>
</div>
</div>
<div class="col-lg-8">
<!-- Basic Card Example -->
<div class="card shadow mb-4 text-right">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-gray-800">new result</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="mark-list">
<thead>
<tr>
<th>subject code</th>
<th>subject</th>
<th>mark</th>
<th>delete</th>
</tr>
</thead>
<tbody>
<?php if(isset($id)): ?>
<?php
$items=$conn->query("SELECT r.*,s.subject_code,s.subject,s.id as sid FROM result_items r inner join subjects s on s.id = r.subject_id where result_id = $id order by s.subject_code asc");
while($row = $items->fetch_assoc()):
?>
<tr data-id="<?php echo $row['sid'] ?>">
<td><input type="hidden" name="subject_id" value="<?php echo $row['subject_id'] ?>"><?php echo $row['subject_code'] ?></td>
<td><?php echo ucwords($row['subject']) ?></td>
<td><input type="hidden" name="markr[]" value="<?php echo $row['mark'] ?>"><?php echo $row['mark'] ?></td>
<td class="text-center"><button class="btn btn-sm btn-danger" type="button" onclick="$(this).closest('tr').remove() && calc_ave()"><i class="fa fa-times"></i></button></td>
</tr>
<?php endwhile; ?>
<script>
$(document).ready(function(){
calc_ave()
})
</script>
<?php endif; ?>
</tbody>
</table>
</div>
<input type="hidden" name="token" value="<?php echo Token::create(); ?>">
<p align="center"><input type="submit" name="insert" class="btn btn-primary" value="insert"></p>
</form>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$('#student_id').change(function(){
var class_id = $('#student_id option[value="'+$(this).val()+'"]').attr('data-class_id');
var _class = $('#student_id option[value="'+$(this).val()+'"]').attr('data-class');
$('[name="class_id"]').val(class_id)
$('#class').text("selected: "+_class);
})
$('#add_mark').click(function(){
var student_id = $('#student_id').val()
var subject_id = $('#subject_id').val()
var mark = $('#mark').val()
if(subject_id == '' || mark == '' || student_id == '' ){
alert_toast("Please select subject & enter a mark before adding to list.","error");
return false;
}
var sData = $('#subject_id option[value="'+subject_id+'"]').attr('data-json')
sData = JSON.parse(sData)
if($('#mark-list tr[data-id="'+subject_id+'"]').length > 0){
alert_toast("Subject already on the list.","error");
return false;
}
var tr = $('<tr data-id="'+subject_id+'"></tr>')
tr.append('<td><input type="hidden" name="subject_id[]" value="'+subject_id+'">'+sData.subject_code+'</td>')
tr.append('<td>'+sData.subject+'</td>')
tr.append('<td class="text-center"><input type="hidden" name="mark[]" value="'+mark+'">'+mark+'</td>')
tr.append('<td class="text-center"><button class="btn btn-sm btn-danger" type="button" onclick="$(this).closest(\'tr\').remove() && calc_ave()"><i class="fa fa-times"></i></button></td>')
$('#mark-list tbody').append(tr)
$('#subject_id').val('').trigger('change')
$('#mark').val('')
calc_ave()
})
</script>
<?php include_once 'footer.php'; ?>