【问题标题】:PHP while loop with edit/delete modal only returns the ID in the first row带有编辑/删除模式的 PHP while 循环仅返回第一行中的 ID
【发布时间】:2015-05-08 11:54:18
【问题描述】:

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

表单链接到另一个页面 (具体来说,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");   

?>

请帮助我。

【问题讨论】:

  • while( $get_row = mysql_fetch_assoc($sql)) { var_dump($get_row); } > 输出是什么
  • 你只需要$iClass 第一行吗?
  • 您的查询在哪里 SELECT ??

标签: php mysql database twitter-bootstrap


【解决方案1】:

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

【讨论】:

  • 我应该把id放在这里吗? &lt;input type="submit" class="btn btn-danger" name="Delete" value="Delete"&gt;
猜你喜欢
  • 2017-11-09
  • 2023-03-16
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2014-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多