我在使用php进行模态时遇到麻烦

时间:2019-01-22 15:27:50

标签: php bootstrap-modal

我有一个表格,其中有一个“编辑”和“删除”按钮,问题是我正在使用模式,一旦在其中一行上单击“编辑”按钮,模式就不会出现,而只会出现在表格的第一行我的桌子...我的代码中缺少什么?

这是我的新闻HTML代码                                                                         

新闻|添加

                                

                                                                       

                                 ?>
                                  <tr>
                                    <td><?php $news_id = $sqrow['news_id']; echo $news_id; ?></td>
                                    <td><?php echo $sqrow['news']; ?> <br>(<?php echo date('l, M d, Y', strtotime($sqrow['news_date'])); ?>, <?php echo date('g:i A', strtotime($sqrow['news_time'])); ?>)</td>
                                    <td>
                                      <div id="newS">
                                        <div id="newSS">
                                          <div id="eq">
                                            <a method="post" data-toggle="modal" data-target="#EDITnews<?php echo $news_id;?>" class="col-sm-8 btn btn-default btn-success btn-block btn-danger btn-sm"><i class="fa fa-edit"></i></a>
                                          </div>
                                          <div id="eq1">
                                            <a href = "deletenews.php?id=<?php echo $sqrow['news_id'];?>" method = "post" class="col-sm-8 btn btn-default btn-block btn-danger btn-sm"><i class="fa fa-close"></i></a>
                                          </div>
                                        </div>
                                      </div>
                                    </td>
                                  </tr>
                                <?php } ?>
                              </tbody>
                            </table> </p>
                          </div>

这是我的编辑模式代码

  <!-- Modal for Edit button for News -->
<div class="modal fade" id="EDITnews<?php echo $news_id;?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="myModalLabel">News</h4>
                </div>
                <div class="modal-body">
                <div class="container-fluid">
                <form method="POST" action="#" enctype="multipart/form-data">
                <?php
                    $conn = mysqli_connect('localhost', 'root', '', 'testdb');
                    $sql = mysqli_query($conn,"SELECT * FROM news WHERE news_id = '$news_id' ");
                    $sqrow = mysqli_fetch_assoc($sql);
                ?>
                     <label><b>News Description:</b></label>
                     <input class="form-control" type="text" value="<?php echo $sqrow['news']; ?>" name="news" required>
                     <label><b>Date:</b></label>
                              <input class="form-control" type="date" value="<?php echo $sqrow['news_date']; ?>" name="newsDate" required>
                     <label><b>Time:</b></label>
                              <input class="form-control" type="time" value="<?php echo $sqrow['news_time']; ?>" name="newsTime" required><br> 
                              <button type="submit" name="addEvent" class="btn btn-primary"><i class="fa fa-save"></i> Save Changes</button>
                </form>                   
                </div>
            </div>
        </div>
    </div>
</div>
<!-- End of Modal for Edit button for News -->

1 个答案:

答案 0 :(得分:0)

您需要遍历结果:

                        <?php
                            $conn = mysqli_connect('localhost', 'root', '', 'testdb');
                            $sql = mysqli_query($conn,"SELECT * FROM news WHERE news_id = '$news_id' ");
                            while($sqrow = mysqli_fetch_assoc($sql)){
$news_id = $sqrow["$news_id"];
                        ?>

        <div class="modal fade" id="EDITnews<?php echo $news_id;?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title" id="myModalLabel">News</h4>
                        </div>
                        <div class="modal-body">
                        <div class="container-fluid">
                        <form method="POST" action="#" enctype="multipart/form-data">

                             <label><b>News Description:</b></label>
                             <input class="form-control" type="text" value="<?php echo $sqrow['news']; ?>" name="news" required>
                             <label><b>Date:</b></label>
                                      <input class="form-control" type="date" value="<?php echo $sqrow['news_date']; ?>" name="newsDate" required>
                             <label><b>Time:</b></label>
                                      <input class="form-control" type="time" value="<?php echo $sqrow['news_time']; ?>" name="newsTime" required><br> 
                                      <button type="submit" name="addEvent" class="btn btn-primary"><i class="fa fa-save"></i> Save Changes</button>
                        </form>                   
                        </div>
                    </div>
                </div>
            </div>
        </div>

    <?php  
    }
    ?>