Untitled

mail@pastecode.io avatar
unknown
php
2 months ago
14 kB
9
Indexable
Never
#this block is for the login session validation
# the other sql queries is for the rest of the php code, all these codes is on the frontend

session_start();

if (isset($_SESSION['sessionn']) && !empty($_SESSION['class'])) {
   $class = $_SESSION['class'];
   $sessionn = $_SESSION['sessionn'];
   $term = $_SESSION['term'];


include 'db-conn.php';

$Tsql = "SELECT id FROM `terms` WHERE name = '$term'";
   $Tquery = mysqli_query($conn, $Tsql);
   $Tresult = mysqli_fetch_assoc($Tquery);
 $Term = $Tresult["id"];


 $Ssql = "SELECT id FROM `sessions` WHERE name = '$sessionn'";
   $Squery = mysqli_query($conn, $Ssql);
   $Sresult = mysqli_fetch_assoc($Squery);
 $Sessionn = $Sresult["id"];

 $Csql = "SELECT id FROM `classes` WHERE class = '$class'";
 $Cquery = mysqli_query($conn, $Csql);
 $Cresult = mysqli_fetch_assoc($Cquery);
 $Class= $Cresult["id"];




// Query to get distinct subjects for the specified class
$subjects_query = "SELECT DISTINCT subjects.name AS subject_name
                   FROM students_subjects
                   INNER JOIN subjects ON students_subjects.subject_id = subjects.id
                   WHERE students_subjects.class = $Class AND students_subjects.term_id = $Term
                    AND students_subjects.session_id = $Sessionn";

$subjects_result = $conn->query($subjects_query);


// Query to get distinct subjects for the specified class
$subjects1_query = "SELECT DISTINCT subjects.name AS subjectt_name
                   FROM students_subjects
                   INNER JOIN subjects ON students_subjects.subject_id = subjects.id
                   WHERE students_subjects.class = $Class AND students_subjects.term_id = $Term
                    AND students_subjects.session_id = $Sessionn";

$subjects1_result = $conn->query($subjects1_query);












#The scores are displaying fine but i have not added an input to this one yet

 <form action="" method="get">
               <div class="row">
                  <div class="col-sm-12">
                    <div class="card card-table">
                        <div class="card-body">
                        <h4 class="form-title"><span>Ca Scores</span></h4>
                            <div class="table-responsive">
                              <?php
                              if ($subjects_result->num_rows > 0) {
                                 $subjects = array();
                                 while ($row = $subjects_result->fetch_assoc()) {
                                     $subjects[] = $row['subject_name'];
                                 }
                             
                                 // Get student names
                                 $students_query = "SELECT students.id, students.student_firstname
                                                    FROM students
                                                    INNER JOIN students_subjects ON students.id = students_subjects.student_id
                                                    WHERE students_subjects.class = $Class AND students_subjects.term_id = $Term
                                                    AND students_subjects.session_id= $Sessionn
                                                    GROUP BY students.id";
                             
                                 $students_result = $conn->query($students_query);
                             
                                 if ($students_result->num_rows > 0) {
                                     echo '<table class="table table-hover table-center mb-0 datatable"><tr><th>Student Name</th>';
                             
                                     // Table headers for subjects
                                     foreach ($subjects as $subject) {
                                         echo '<th>' . htmlspecialchars($subject) . '</th>';
                                     }
                                     echo '</tr>';
                             
                                     // Display student names and their scores
                                     while ($student_row = $students_result->fetch_assoc()) {
                                         echo '<tr><td>' . htmlspecialchars($student_row['student_firstname']) . '</td>';
                             
                                         foreach ($subjects as $subject) {
                                             $student_id = $student_row['id'];
                             
                                             $score_query = "SELECT ca_score
                                                             FROM students_subjects
                                                             INNER JOIN subjects ON students_subjects.subject_id = subjects.id
                                                             WHERE students_subjects.class = $Class  AND students_subjects.term_id = $Term
                                                    AND students_subjects.session_id= $Sessionn AND students_subjects.student_id = '$student_id' AND subjects.name = '$subject'";
                             
                                             $score_result = $conn->query($score_query);
                             
                                             if ($score_result->num_rows > 0) {
                                                 $score_row = $score_result->fetch_assoc();
                                                 echo '<td><input class="form-control  text-center" value="' .htmlspecialchars($score_row['ca_score']). '" /></td>';
                                                 
                                             } else {
                                                 echo '<td>N/A</td>';
                                                
                                             }
                                         }
                                         echo '</tr>';
                                     }
                             
                                     echo '</table>';
                                 } else {
                                     echo "No students found for this class.";
                                 }
                             } else {
                                 echo "No subjects found for this class.";
                             }
                            
                              ?>
                            </div>
                        </div>

                    </div>
                  </div>
               </div>
                            </form>
 
 
 
 #This is the one I'm working with for now, it also works fine, it retrieves the data fine
 but then I created an input so I can submit the form to update the scores. It shows update successful
 but the db doesn't update. 

            <form id="editExamForm">            
               <div class="row">
                  <div class="col-sm-12">
                    <div class="card card-table">
                        <div class="card-body">
                           <h4 class="form-title"><span>Exam Scores</span></h4>
                            <div class="table-responsive">
                              <?php
                              if ($subjects1_result->num_rows > 0) {
                                 $subjects1 = array();
                                 while ($row1= $subjects1_result->fetch_assoc()) {
                                     $subjects1[] = $row1['subjectt_name'];
                                 }
                             
                                 // Get student names
                                 $students_query = "SELECT students.id, students.student_firstname
                                                    FROM students
                                                    INNER JOIN students_subjects ON students.id = students_subjects.student_id
                                                    WHERE students_subjects.class = $Class AND students_subjects.term_id = $Term
                                                    AND students_subjects.session_id= $Sessionn
                                                    GROUP BY students.id";
                             
                                 $students_result = $conn->query($students_query);
                             
                                 if ($students_result->num_rows > 0) {
                                     echo '<table class="table table-hover table-center mb-0 datatable"><tr><th>Student Name</th>';
                             
                                     // Table headers for subjects
                                     foreach ($subjects1 as $subject1) {
                                        echo '<th>' . 'IDDDDDDDD' . '</th>';
                                         echo '<th>' . htmlspecialchars($subject1) . '</th>';
                                     }
                                     echo '</tr>';
                             
                                     // Display student names and their scores
                                     while ($student_row = $students_result->fetch_assoc()) {
                                         echo '<tr><td>' . htmlspecialchars($student_row['student_firstname']) . '</td>';
                             
                                         foreach ($subjects as $subject) {
                                             $student_id = $student_row['id'];
                             
                                             $score_query = "SELECT student_id, exam_score
                                                             FROM students_subjects
                                                             INNER JOIN subjects ON students_subjects.subject_id = subjects.id
                                                             WHERE students_subjects.class = $Class  AND students_subjects.term_id = $Term
                                                    AND students_subjects.session_id= $Sessionn AND students_subjects.student_id = '$student_id' AND subjects.name = '$subject'";
                             
                                             $score_result = $conn->query($score_query);
                             
                                             if ($score_result->num_rows > 0) {
                                                 $score_row = $score_result->fetch_assoc();
                                                 echo  '<td><input class="form-control text-center" name="ids[]" value="' .htmlspecialchars($score_row['student_id']). '" /></td>';
                                                 echo '<td><input class="form-control text-center" name="scores[]" value="' .htmlspecialchars($score_row['exam_score']). '" /></td>';
                                             } else {
                                                 echo '<td>N/A</td>';
                                             }
                                         }
                                         echo '</tr>';
                                     }
                             
                                     echo '</table>';
                                 } else {
                                     echo "No students found for this class.";
                                 }
                             } else {
                                 echo "No subjects found for this class.";
                             }
                              
                              ?>
                            </div>
                        </div>

                    </div>
                    <input class="form-control" type="submit" value="Update">
                  </div>
               </div>
                            </form>
                            
                            
                            
                            
        #This is the backend code to process the update                    
                            
                            
           <?php

session_start();
if (isset($_SESSION['sessionn']) && !empty($_SESSION['class'])) {
   $class = $_SESSION['class'];
   $sessionn = $_SESSION['sessionn'];
   $term = $_SESSION['term'];
   
// Connect to your database
#include('db-conn.php');

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Sch_mgmt";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}


$response = array(
    'status' => 0,
    'message' => 'Form submission failed, please try again.'
);



$id = $_POST['ids'][0];
$scores = $_POST['scores'][1];


// Check if the form has been submitted

  // Iterate through the form inputs using a nested loop
  foreach ($_POST['ids'] as $index => $id) {
    // Update the "exam_score" column in the database
    $sql = "UPDATE students_subjects SET exam_score = '".$_POST['scores'][$index]."'  WHERE student_id = $id";
    $result = mysqli_query($conn, $sql);

    // Check for errors
    if (!$result) {
        $response['status'] =  0;
         $response['message']  = "Error updating database for input";

     # die("Error updating database for input $id: " . mysqli_error($conn));
    }
  }

  // Overall success message
  $response['status'] =  1;
  $response['message']  = "All data updated successfully.";
}

echo json_encode($response);

// Close the database connection
mysqli_close($conn);




?>



                 
                            
Leave a Comment