簡體   English   中英

PHP MySqli 如何從我不知道名稱的表中選擇所有內容?

[英]PHP MySqli How to SELECT Everything from a Table for which i don't know the name?

我正在做一個個人項目,比如文檔管理系統。 我正在數據庫中創建與文件夾同名的新文件夾和表。

我可以列出所有這些,並且當我單擊 HTML 表中列出的表名稱時,我希望顯示該表中的所有內容。

但我不知道桌子的名字。

我得到“ 0結果”。

但是我可以在 URL 中看到我在“正確”表中,因為它顯示了我的表的名稱。

我知道問題出在 view.php 的 SELECT 語句中,我不知道如何選擇我不知道名稱的表:-)

有人可以幫幫我嗎?

非常感謝。

這是我的索引代碼,我將 DB 中的所有表列為按鈕:

<div class="container-fluid">
  <div class="row">

<div class="col-md-4 col-xl-4 text-center ml-sm-2 ml-md-5 ml-lg-5 mt-5">
  <div class="alert alert-danger mb-0"><strong>TABLES from DATABASE:</strong></div>

    <table class="table table-bordered table-hover text-center">

      <tr class="title">
        <th>Folder Name:</th>
      </tr>

  <?php

    $conn = mysqli_connect("localhost", "root", "", "folder");

    if (!$conn) {
       die("Connection failed: " . mysqli_connect_error());
    }

     $sql = "SELECT table_name FROM information_schema.tables where table_schema='folder';";
     $result = $conn-> query($sql);

      if ($result) {
      while ($row = $result-> fetch_assoc()) {


      echo "<a href='view.php?". $row['table_name'] ."' ><span style='font-size: 19px; color: #3277b6; margin-right: 15px;'><i class='far fa-eye'></i></span></a>";


    }
              }
          else {
            echo "0 results";
    }

    $conn-> close();

    ?>

    </table>
</div>

這是 view.php 代碼:

<body>

     <div class="container">
      <div class="row mt-5 ml-5">

       <div class="col-md-8 col-xl-11 text-center ml-sm-2 ml-md-5 ml-lg-5 mt-5">
         <div class="alert alert-danger mb-0">
           <a href="index.php" class="btn btn-dark mr-5" role="button">Go Back</a>
      </div>

       <div style="overflow-x:auto;">
         <table class="table table-bordered table-hover text-center">
            <tr>
              <th>Name:</th>
               <th>Create at:</th>
            </tr>

<?php

$conn = mysqli_connect("localhost", "root", "", "folder");

if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM information_schema.tables where table_name = ?";
$result = $conn-> query($sql);

if ($result) {
while ($row = $result-> fetch_assoc()) {
echo "<tr>
         <td>".$row['name']."</td>
         <td>".$row['created_at']."</td>
      </tr>";
}
echo "</table>";
}
else {
echo "0 results";
}

$conn-> close();

?>

                      </table>

                      </div>

                      </div>
            </div>
        </div>

        <!-- Optional JavaScript -->
        <!-- jQuery first, then Popper.js, then Bootstrap JSs -->
        <script src="js/jquery-3.3.1.min.js"></script>
        <script src="js/bootstrap.bundle.min.js"></script>
        <script src="js/theme-script.js"></script>

</body>
</html>

更改以下行:

echo "<a href='view.php?". $row['table_name'] ."' ><span style='font-size: 19px; color: #3277b6; margin-right: 15px;'><i class='far fa-eye'></i></span></a>";

echo "<a href='view.php?table_name=". $row['table_name'] ."' ><span style='font-size: 19px; color: #3277b6; margin-right: 15px;'><i class='far fa-eye'></i></span></a>";

然后更改以下行:

$sql = "SELECT * FROM information_schema.tables where table_name = ?";

$sql = "SELECT * FROM information_schema.tables where table_name = '{$_REQUEST['table_name']}'";

需要注意的是,這是一個相當大的安全風險,您需要確保在將變量注入 SQL 代碼之前已清除該變量。

最后一件事,你實際上並沒有做任何錯誤捕捉,所以改變這個:

else {
echo "0 results";
}

else {
echo $conn->error;
}
$sql = "SELECT " .$varible. " FROM information_schema.tables where table_schema='folder'";
$result = $conn->query($sql);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM