What's the problem with data that doesn't go into database؟

When I send data to the database from the form, it does not receive both values in subject_id and mark field
2021-02-21_122807

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'; ?>
1 Like

Those values appear to be coming from the $_POST array. That would be the first place to check.

What’s the solution?

I can’t say. What is the result of dumping the $_POST, are the values what you expect?

When I use javascript, the problem occurs but without javascript data goes into the database

            <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>

I would guess js may be the problem then.
I was thinking of moving this to PHP, but perhaps the JS forum would be better.

I hope there’s someone to help me.

1 Like

The developer tools of the web browser can help you to figure out what’s happening.

What does the Network panel of your developer tools show that the form data is, when you send the data?

Don’t reuse name attributes in this way. Is subject_id supposed to be a value or an array? Which is getting submitted by the form?

2 Likes

Thank you. I’m trying now.
I’ll change duplicate name

I did that you suggested to me, I changed subject_id to subid
It wasn’t solved.
2021-02-21_145617

<?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']);
                          $subid=@output($_POST['subid']);
                          $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,:subid,:mark);");
                          //$stm2->bindParam(":id", $id, PDO::PARAM_STR);
                          $stm2->bindParam(":result_id", $result_id, PDO::PARAM_STR);
                          $stm2->bindParam(":subid", $subid, 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="subid"  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="subid" 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; ?>

                        <?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="subid[]" 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('')

              })

            </script>
<?php include_once 'footer.php'; ?>

You havent fixed the issue, you just did a mass replace. These three things. they must all have DIFFERENT name values.

can you give me an example to do it

Mkay…

I did that, bro.
The problem wasn’t solved.

<?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']);
                          $c=@output($_POST['c']);
                          $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,:c,:mark);");
                          //$stm2->bindParam(":id", $id, PDO::PARAM_STR);
                          $stm2->bindParam(":result_id", $result_id, PDO::PARAM_STR);
                          $stm2->bindParam(":c", $c, 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="c" 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="a" 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; ?>

                        <?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="b[]" 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('')

              })

            </script>
<?php include_once 'footer.php'; ?>

What type of field is subject_id in the database?

2021-02-21_155631

right… but is it a varchar? an int? a boolean?

rather surprised its correctly handling the strings you’re passing it then…
What happens if you add this line before your prepare?

echo "C is $c";