提问者:小点点

如何在PHP中添加2个不同的查询?


当我将两个Sql查询放在一个页面中时,我在显示表时遇到了问题。 我没有这个问题,当我只放1个查询。

我想从student table中显示学生的列表,讲师可以添加需要他们照顾的学生,这将具有添加功能。 我已经做了编码,但我遇到了问题。

这是我的密码


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

我注意到,当我将此查询放入表中时,表中没有显示学生的完整列表


    <?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'];
                                      ?>

我想要实现的是我想要在数据库中显示所有已经注册的学生,并且讲师可以添加学生,学生信息连同讲师的id和姓名将被保存到数据库中。 如何修复这个问题?


共1个答案

匿名用户

您正在使用$fetch从这两个查询中获取结果。

下面是代码中发生的情况。

  1. 您的$fetch从学生表中获取结果。
  2. 显示表内容,直到它到达第二个查询。
  3. 执行第二个查询时,$fetch值将更改。 因此,学生表只显示1行。

将第二个查询中的$fetch更改为其他内容,如$fetch2