带有编辑/删除模式的PHP while循环仅返回第一行中的ID

时间:2015-05-08 03:18:03

标签: php mysql database twitter-bootstrap

我使用php while循环显示了一个记录表。 此外,每行都有一个相应的按钮,即删除和编辑按钮。我为按钮使用了一定的bootstrap模态。

表单链接到另一个页面 (具体来说,success.php?id =) 旨在通过其URL传递特定行的ID。但我的问题是传递的ID将始终是第一行的ID。

所以这是我桌子的代码。

<?php
                    $sql = mysql_query("SELECT * 
                                        FROM foreignlanguage
                                        INNER JOIN sched
                                        ON sched.sched_id = foreignlanguage.schedID 
                                        WHERE sched.type='Group' and foreignlanguage.Subject = 'Italian' and 
                                        sched.del='1' and (foreignlanguage.area = 'A1' OR foreignlanguage.area = 'A2' OR foreignlanguage.area = 'A3' OR foreignlanguage.area = 'A4' OR foreignlanguage.area = 'A5')");
                    $count = mysql_num_rows($sql);
                    if($count != 0)
                    {
                ?>

    <table class="table table-striped table-hover table-bordered" id="editable-sample">
                            <thead>
                            <tr>
                                <th><div style="width: 45px" >Level</div></th>
                                <th><div style="width: 60px" >Days</div></th>
                                <th><div style="width: 70px" >Time</div></th>
                                <th><div style="width: 90px" >Teacher</div></th>
                                <th><div style="width: 10px" >Room</div></th>
                                <th><div style="width: 10px" >Hours</div></th>
                                <th><div style="width: 15px" >Price</div></th>
                                <th><div style="width: 10px" >Options</div></th>
                            </tr>
                            </thead>
                            <tbody>
                                <?php
                                    while( $get_row = mysql_fetch_assoc($sql))
                                    {
                                        $iClass = $get_row['sched_id'];
                                        $iDay = $get_row['day'];
                                        $iTime = $get_row['time'];
                                        $iTeacher = $get_row['teacher'];
                                        $iRoom = $get_row['room'];
                                        $iHour = $get_row['hour'];
                                        $iPrice = $get_row['price'];
                                        $iLevel = $get_row['area'];
                                        $del = $get_row['del'];
                                ?>

                                <tr class="">
                                    <td><?php echo $iLevel; ?></td>
                                    <td><?php echo $iDay; ?></td>
                                    <td><?php echo $iTime; ?></td>
                                    <td><?php echo $iTeacher; ?></td>
                                    <td><?php echo $iRoom; ?></td>
                                    <td><?php echo $iHour; ?></td>
                                    <td><?php echo $iPrice; ?></td>
                                    <td>
    <!-- Delete MODAL-->
                                        <?php 
                                            if(isset($_POST['Delete']))
                                            {
                                                header("Location: success.php");
                                            }
                                        ?>

                                        <a href="#delModal" class="btn btn-mini btn-danger" data-toggle="modal" data-target="#delModal"><i class="icon-remove icon-white"></i> Delete</a>
                                        <div class="modal fade" id="delModal" tabindex="-1" role="dialog" aria-labelledby="delModal" aria-hidden="true">
                                        <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                                <h4 class="modal-title" id="myModalLabel">Delete Schedule</h4>
                                            </div>
                                            <div class="modal-body">
        <!--try-->                                      
                                                Are you sure that you want to delete this schedule?
        <!--/try-->
                                            </div>
                                            <div class="modal-footer">
                                                <form method='post' action='success.php?id=<?php echo $iClass; ?>' class="form-horizontal">
                                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                                    <input type="submit" class="btn btn-danger" name="Delete"  value="Delete"> 
                                                </form>         
                                            </div>
                                        </div>
                                        </div>
                                        </div>
    <!-- END DELETE MODAL -->
                                    </td>
                                </tr> 
                                <?php 
                                    }
                        }
                        else
                        {
                            echo "<h4>There are no schedules available yet</h4>";
                        } 
                                ?>
                        </tbody>
                    </table>

这里是链接页面。

<?php include('includes/init.php');

$del_this = $_GET['id'];    
$delsched = mysql_query(" UPDATE sched SET del = '0' WHERE sched_id = '$del_this' ") or die(mysql_error("ERROR"));
$delsched_list = mysql_query("UPDATE sched_list SET val = '0' WHERE sched_id = '$del_this' ") or die(mysql_error("ERROR"));

echo $del_this;
header("Location: admin-foreign.php");   

?>
请帮助我。

1 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点。无论如何,您需要为您的按钮设置唯一的ID,因此您可以将它们设为Id="EditButton".$rowid,同样删除