Categories
Mastering Development

How to view data of specific row in database when i click specific row in table html php mysql

Here is my coding. I want to view data when i click any row on my table. But it view only the first data on row. What do you think is the problem?

My code on retrieving data from database is working. But when i click it, it always view the data of the first retrieved data row.

<?php

$sql = "SELECT * FROM courses";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        echo "<table class='table table-bordered' id='myTable'>";
            echo "<tr>";
                echo "<th>COURSE CODE</th>";
                echo "<th>COURSE NAME</th>";
                echo "<th>ACTION</th>";
            echo "</tr>";
        while($row = mysqli_fetch_array($result)){
            echo "<tr>";
                echo "<td>" . $row['course_code'] . "</td>";
                echo "<td>" . $row['course_desc'] . "</td>";
                echo "<td>";
                    echo "<a href='#' data-toggle='modal' data-target='#confirmenrollment'>";
                        echo "Enroll";
                    echo "</a>";
                echo "</td>";   
            echo "</tr>";


            echo "<div class='modal fade' id='confirmenrollment' tabindex='-1' role='dialog'>";
                echo "<div class='modal-dialog'>";
                    echo "<div class='modal-content'>";
                        echo "<div class='modal-header'>";
                            echo "<button type='button' class='close' data-dismiss='modal'>&times;</button>";
                            echo "<h3>Confirm course to enroll.</h3><br>";
                            echo "<p><b>Course Name:&nbsp&nbsp </b>" . $row['course_desc'] . "</p>";
                            echo "<p><b>Course Code:&nbsp&nbsp </b>" . $row['course_code'] . "</p>";
                            echo "<p><b>Price:&nbsp&nbsp </b>" . $row['course_price'] . "</p>";
                                echo "<div class='more-button'>";
                                    echo "<a href='#'>";
                                        echo "Confirm Enrollment";
                                    echo "</a>";
                                echo "</div>";
                        echo "</div>";
                    echo "</div>";
                echo "</div>";
            echo "</div>";

        }
        echo "</table>";

        mysqli_free_result($result);
    } else{
        echo "No courses found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

}


mysqli_close($link);
?>

Can anyone help me on this?

Leave a Reply

Your email address will not be published. Required fields are marked *