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

标签 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");   

?>

请帮助我。

最佳答案

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

也是如此

关于具有编辑/删除模式的 PHP while 循环仅返回第一行中的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30115180/

相关文章:

php - 在 Amazon EC2 Linux AMI 上安装 FFMPEG-Php

php - SELECT DISTINCT 有什么问题?

php - CakePHP 3.0 在模板中回显 css 文件的内容

php - 在 PHP 中使用 WAMP 服务器记录错误

php - 想知道.. mysql 和 mysqli 是否可以同时在同一个网站上使用?

mysql - 更新本地数据库所有修改的数据到服务器数据库

mysql - 如何将 mysql2 连接到 Ruby on Rails?

java - 只需在 BDD (Hibernate) 中插入日期的 "Hour:Minutes"

mysql - 如何将 MySql 联合查询的结果从 2 列拆分为 4

database - Cassandra 与 ScyllaDB 的内存使用情况