使用php从模态内的第二个sql表中检索数据

时间:2018-07-31 14:05:36

标签: php sql

问题是,如果数据位于第二个名为locationstbl的表中,并且该表在sql中的地址为列,那么如何在模态内检索Locatie(下拉列表)的数据。

也许有某种方式可以使用JOIN或类似的方式来显示我在该下拉列表中需要的数据?

谢谢。

这是代码。

<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
          <thead>
            <tr>
              <th>#</th>
              <th>Locatie</th>
              <th>Serie Aparat</th>
              <th>Tip Joc</th>
              <th>Cabinet</th>
              <th>Data Expirare</th>
              <th>Actiuni</th>
            </tr>
          </thead>

          <tbody>



 <?php


    $sql = "SELECT location, serial, game_type, cabinet, date 
            FROM all_machines 
            ORDER BY location ASC";
    $result = $conn->query($sql);
    $resultNum = mysqli_num_rows($result);
    $counter = 1;
    if ($resultNum > 0) {
        while ($row  = mysqli_fetch_assoc ($result)){
            $location = $row['location'];
            $serial = $row['serial'];
            $game_type = $row['game_type'];
            $cabinet = $row['cabinet'];
            $date = $row['date'];



?>
        <tr>
            <td><?php echo $counter++;?></td>
            <td><?php echo $location;?></td>
            <td><?php echo $serial;?></td>
            <td><?php echo $game_type;?></td>
            <td><?php echo $cabinet;?></td>
            <td><?php echo $date;?></td>
            <td>    

                    <!-- Table Action Buttons -->
                <a href="#editare<?php echo $serial;?>" data-toggle="modal">
                    <button type='button' class='btn btn-success btn-sm' data-whatever="<?php echo $serial;?>">Editare</button></a> 

                <a href="#delete<?php echo $serial;?>" data-toggle="modal">
                    <button type='button' class='btn btn-danger btn-sm' >Sterge</button></a>

                <a href='detalii.php?id=<?php echo $serial;?>'>
                    <button type='button' class='btn btn-info btn-sm'>Detalii</button></a>

            </td>


        <!--Delete Modal -->
<div id="delete<?php echo $serial; ?>" class="modal fade" role="dialog">                     
        <div class="modal-dialog">
            <!-- Modal content-->
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">Confirmati stergerea</h4>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
                    <div class="modal-body">
                        <input type="hidden" name="serial" value="<?php echo $serial; ?>">
                        <div class="alert alert-danger"><p>Sunteti sigur ca doriti stergerea seriei <strong><?php echo $serial; ?></strong> ?</p>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <a class='btn btn-danger btn-sm' href="delete.php?id=<?php echo $serial;?>">Sterge</a>
                        <button type="button" class="btn btn-primary btn-sm" data-dismiss="modal">Anuleaza</button>
                    </div>
                </div>
        </div>
</div>



                <!--Edit Modal -->
<div id="editare<?php echo $serial; ?>" class="modal fade" role="dialog">                     
        <div class="modal-dialog" role="document">
            <!-- Modal content-->
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">Editare Serie: <strong><?php echo $serial; ?></strong></h4>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
                    <div class="modal-body">
                        <form method="POST" action="edit-mac.php?id=<?php echo $serial;?>">
                            <div class="form-group">
                                <div class="form-row">
                                    <!-- Select data from locationstbl -->
                                    <div class='col-md-6'>
                                        <label>Locatie</label>
                                        <select name='location' class='form-control input-sm'>
                                            <option></option>
                                        </select>
                                        <span class='help-block'></span>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="serial">Serie</label>
                                        <input type="text" class="form-control" id="serialup" name="serialup" value="<?php echo $serial; ?>">
                                        <span class="help-block"></span>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="serial">Tip Joc</label>
                                        <input type="text" class="form-control" id="game_type" name="game_type" value="<?php echo $game_type; ?>">
                                        <span class="help-block"></span>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="serial">Cabinet</label>
                                        <input type="text" class="form-control" id="cabinet" name="cabinet" value="<?php echo $cabinet; ?>">
                                        <span class="help-block"></span>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="date">Data expirare</label>
                                        <input class="form-control" id="date" type="text" autocomplete="off" name="date" value="<?php echo $date; ?>" placeholder="LL-AAAA">
                                        <span class="help-block"></span>
                                    </div>
                                </div>
                            </div>    
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-success btn-sm">Actualizeaza</button>
                        <button type="button" class="btn btn-primary btn-sm" data-dismiss="modal">Anuleaza</button>
                        </form>
                    </div>
                </div>
        </div>
</div>



        </tr>
<?php
    }
}
?>   


          </tbody>     
        </table>

1 个答案:

答案 0 :(得分:1)

在给出可能的答案之前,我想提醒您开始考虑采用PHP开发框架并充分利用其优势。从长远来看,将PHP与HTML混合使用会适得其反,并且难以维护。 您有两种方法可以做到:

  1. 像第二张表一样对第二张表执行SQL查询,并仅获取下拉列表的信息:$locaties = "SELECT * FROM locationstbl" 通过此解决方案,您所在的下拉菜单将如下所示:
<div class='col-md-6'>
    <label>Locatie</label>
    <select name='location' class='form-control input-sm'>
        <?php foreach($locaties as $locatie){ ?>
            <option><?php echo $locatie ?></option> 
        <?php } ?>
    </select>
    <span class='help-block'></span>
</div>
  1. 使用INNER JOIN进行一次搜索,只要有外键,就可以从位置表中获取信息+ locationtbl表信息。