繁体   English   中英

PHP 使用 MYSQL 表结果作为另一个查询的参数

[英]PHP use MYSQL table result as parameter for another query

我的网站上有一个基于搜索从 MYSQL 中提取的表。 我希望能够单击添加到表中的按钮,该按钮会根据所选行打开更多数据。 在下图中,我有一列标记为第一个表的 ID,可用于 = ? 在我的查询中,但我不知道如何首先将值设置为参数。 对解决此问题的最佳方法有任何想法吗?

我有$term = '%' . $_GET['itemID'] . '%'; $term = '%' . $_GET['itemID'] . '%'; 为第二个查询添加,但需要一种基于相应视图按钮绑定 ID 999999、1000000 或 1000001 的方法。 谢谢!

第一个表

if(isset($_GET['term']))
    {   
       
        $term = '%' . $_GET['term'] . '%';
        $sql1 = "SELECT ID, Source, Contract_Number, Price_Effective, Price_Expiration, Min_Price, Average_Price
        FROM Both_Search WHERE SearchName LIKE ? LIMIT 10";

        $stmt = $conn->prepare($sql1); 
        $stmt->bind_param("s", $term);   
        $stmt->execute();
        $result1 = $stmt->get_result();
          while($row1 = $result1->fetch_assoc()) {
            echo "<td>" . $row1["ID"] . "</td>"; <----would want this to be the parameter for next query
            echo "<td>" . $row1["Source"] . "</td>";
            echo "<td><a target='_blank' rel='noopener noreferrer' href='contracts.php?number=" . $row1["Contract_Number"] . "'>" . $row1["Contract_Number"] . "</td>";
            echo "<td>" . $row1["Price_Effective"] . "</td>";
            echo "<td>" . $row1["Price_Expiration"] . "</td>";
            echo "<td>" . "$ " . $row1["Average_Price"] . "</td>";
            echo "<td>" . "$ " . $row1["Min_Price"] . "</td>"; ?>
                <td><button id="myBtn" type="button" class="btn btn-primary">View</button></td>
            </tr><?php

             }
            }
            
      
            
         
?> 

</tbody>
                                </table>
                            </div>
                        </div>
                    </div>

                </div>


                <!-- The Modal -->
                <div id="myModal" class="modal">


<!-- Modal content -->
<div class="modal-content">
  <span class="close">&times;</span>

  <div class="card shadow mb-4">
    <div class="card-header py-3" style="background-color: rgb(90, 136, 255);">
        <h6 class="m-0 font-weight-bold text-white">Tiers</h6>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-bordered" id="dataTable2" width="100%" cellspacing="0">
                <thead>
                    <tr>
                        <th>Price 1</th>
                        <th>Price 2</th>
                        <th>Price 3</th>
                        <th>Price 4</th>
                        <th>Price 5</th>
                        <th>Price 6</th>
                        <th>Price 7</th>
                        <th>Price 8</th>
                        <th>Price 9</th>
                    </tr>
                </thead>

                            <tbody>
                            
                                                                      
                                                                      <?php
                                                                      
                                                                        
                                                                      if(isset($_GET['itemID']))
                                                                          {   
                                                                             
                                                                              $term = '%' . $_GET['itemID'] . '%';
                                                                              $sql1 = "SELECT *
                                                                              FROM Pricelist WHERE ID = ? LIMIT 10";
                                                                      
                                                                              $stmt = $conn->prepare($sql1); 
                                                                              $stmt->bind_param("s", $term);   
                                                                              $stmt->execute();
                                                                              $result1 = $stmt->get_result();
                                                                                while($row1 = $result1->fetch_assoc()) {
                                                                                  echo "<td>" . "$ " . $row1["P_1"] . "</td>";
                                                                                  echo "<td>" . "$ " . $row1["P_2"] . "</td>";
                                                                                  echo "<td>" . "$ " . $row1["P_3"] . "</td>";
                                                                                  echo "<td>" . "$ " . $row1["P_4"] . "</td>";
                                                                                  echo "<td>" . "$ " . $row1["P_5"] . "</td>";
                                                                                  echo "<td>" . "$ " . $row1["P_6"] . "</td>";
                                                                                  echo "<td>" . "$ " . $row1["P_7"] . "</td>";
                                                                                  echo "<td>" . "$ " . $row1["P_8"] . "</td>";
                                                                                  echo "<td>" . "$ " . $row1["P_9"] . "</td>";
                                                                                  
                                                                                  ?>
                                                                                    </tr> <?php
                                                                                   }
                                                                                  }
                                                                                  
                                                                                  $conn->close();
                                                                                  
                                                                         
                                                                      ?> 
                                                                             
                                                                      </tbody>  
<script>
    // Get the modal
var modal = document.getElementById("myModal");

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal
btn.onclick = function() {
  modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}


</script>    

您的 ID 为$row1["ID"] ,您可以将该 ID 用于Another Query

// Replace this
<td><button id="myBtn" type="button" class="btn btn-primary">View</button></td>
// To this
<td><a href="YOUR_PAGE_VIEW.php?id=<?= $row1["ID"]; ?>" id="myBtn"  class="btn btn-primary">View</a></td>

并且“查看”按钮将重定向到参数查询 ID = $row1["ID"] 的 YOUR_PAGE_VIEW.php。 现在,您在 YOU_PAGE_VIEW.php 中将 ID 作为变量$id用于另一个查询您想要的内容。

// In YOUR_PAGE_VIEW.php
$id = $_GET['id'];

希望这对你有帮助,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM