-1

I have problem with displaying the table when I put 2 Sql queries inside 1 page. I don't have this problem when I only put 1 query.

I wanted to display the list of student from the student table and the the lecturer can add the student to be put under their care which will have the add function. I have done the coding but I encountered problem.

this is my code


    <table id=" table" class="table table-bordered">
                <thead>
                    <tr>
                        <th>Student ID</th>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Contact Number</th>
                        <th>Programme Type</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        $query = mysqli_query($conn, "SELECT * FROM `student`") or die(mysqli_error());
                        while($fetch = mysqli_fetch_array($query)){
                    ?>

                    <tr class="del_student<?php echo $fetch['student_id']?>">
                            <td><?php echo $fetch['student_id']?></td>
                            <td><?php echo $fetch['student_name']?></td>
                            <td><?php echo $fetch['student_email']?></td>
                            <td><?php echo $fetch['contact_number']?></td>
                            <td><?php echo $fetch['programme_type']?></td>
                            <td><?php echo $fetch['status_type']?></td>
                            <td><center><button class="btn btn-warning" data-toggle="modal" data-target="#edit_modal<?php echo $fetch['student_id']?>"><span class="glyphicon glyphicon-edit"></span> Edit</button> </center>
                            </td>
                    </tr>

    <div class="modal fade" id="edit_modal<?php echo $fetch['student_id']?>" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content">
                    <form method="POST" action="update_student.php">    
                        <div class="modal-header">
                            <h4 class="modal-title">Add Supervisee</h4>
                        </div>
                        <div class="modal-body">
                            <div class="col-md-3"></div>
                            <div class="col-md-6">

                                <div class="form-group">
                                    <label>Student ID</label>
                                    <input type="hidden" name="id" value="<?php echo $fetch['id']?>" class="form-control"/>
                                    <input type="number" name="student_id" value="<?php echo $fetch['student_id']?>" class="form-control" required="required"/>
                                </div>

                                <div class="form-group">
                                    <label>Student Name</label>
                                    <input type="text" name="student_name" value="<?php echo $fetch['student_name']?>" class="form-control" required="required"/>
                                </div>

                                <div class="form-group">
                                    <label>Student Email</label>
                                    <input type="email" name="student_email" value="<?php echo $fetch['student_email']?>" class="form-control" required="required"/>
                                </div>

                                <div class="form-group">
                                    <label>Contact Number </label>
                                    <input type="text" name="contact_number" value="<?php echo $fetch['contact_number']?>" class="form-control" required="required"/>
                                </div>

                                <div class="form-group">
                                    <label>Programme Type</label>
                                    <input type="text" name="programme_type" value="<?php echo $fetch['programme_type']?>" class="form-control" required="required"/>
                                </div>

                                <div class="form-group">
                                    <label>Status</label>
                                    <input type="text" name="status_type" value="<?php echo $fetch['status_type']?>" class="form-control" required="required"/>
                                </div>

                                <label>Supervisor Information</label>

                                <div class="form-group">
                                     <?php
                                          $query = mysqli_query($conn, "SELECT * FROM `supervisor` WHERE `user_id` = '$_SESSION[user]'") or die(mysqli_error());
                                          $fetch = mysqli_fetch_array($query);
                                          $user_id = $fetch['work_id'];
                                      ?>
                                    <label>Supervisor ID</label>
                                    <input type="text" name="supervisor_id" value="<?php echo $fetch['work_id']?>" class="form-control" required="required"/>
                                </div>

                                <div class="form-group">
                                    <label>Supervisor Name</label>
                                    <input type="text" name="supervisor_name" value="<?php echo $fetch['name']?>" class="form-control" required="required"/>
                                </div>

                                <div class="form-group">
                                    <label>Supervisor Role </label>
                                    <select name="supervisor_role" class="form-control" required="required">
                                        <option value="Chose supervisor role"></option>
                                        <option value="Supervisor">Supervisor</option>
                                        <option value="Co - Supervisor">Co-Supervisor</option>
                                    </select>
                                </div>


                            </div>
                        </div>
                        <div style="clear:both;"></div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
                            <button name="update" class="btn btn-warning" ><span class="glyphicon glyphicon-save"></span> Update</button>
                        </div>
                    </form>
                </div>
            </div>

                    <?php
                        }
                    ?>

                </tbody>

            </table>

        </div>
    </div>

I noticed that the table did not display the full list of student when I put this query


    <?php
                                          $query = mysqli_query($conn, "SELECT * FROM `supervisor` WHERE `user_id` = '$_SESSION[user]'") or die(mysqli_error());
                                          $fetch = mysqli_fetch_array($query);
                                          $user_id = $fetch['work_id'];
                                      ?>

What I want to achieve is I want to display all the student that have been registered inside the database, and the lecturer can add the student, student information alongside with the lecturer id and name will be saved into the database. How to fix this problem?

lilin
  • 117
  • 1
  • 2
  • 10
  • You have an error. [`mysqli_error()`](https://www.php.net/manual/en/mysqli.error.php) needs one argument. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Jun 08 '20 at 15:40

1 Answers1

0

You are using $fetch to get results from both queries.

Here's what is happening in your code.

  1. your $fetch gets results from students table.
  2. displays the table content till it reaches the 2nd query.
  3. when your 2nd query gets executed, $fetch values gets changed. Thus only 1 row is displayed for your student's table.

Change the $fetch in 2nd query to something else, like $fetch2

Ford Prefect
  • 74
  • 1
  • 4
  • Thank you it works i just change the `$query` to `$query1` and `$fetch` to `$fetch2` thank you. Now I finally understand why it did not show up. – lilin Jun 08 '20 at 12:33