2017-04-04 120 views
2

我有不同的表,如表1到5在MySQL數據庫中。我想要一個代碼來顯示每個表,當我從下拉列表中選擇它。當我嘗試執行下面我沒有響應和沒有數據加載形式的SQL。如何在php中使用下拉列表顯示不同的mysql表格

<div> 
<form action="table1.php" method="GET"> 
<input list="name" name="name"> 
<datalist id="name"> 
    <option value="table 1"> 
    <option value="table 2"> 
    <option value="table 3"> 
    <option value="table 4"> 
    <option value="table 5"> 
</datalist> 
<input type="submit" name="search" value="search"> 
</form> 
</div> 

<div> 
<table width="600" border="0" cellpadding="1" cellspacing="1"> 
    <tr> 
     <th>Column 1</th> 
     <th>Column 2</th> 
     <th>Column 3</th> 
     <th>Column 4</th> 
    </tr> 
</table> 
在php代碼

我分配用於選擇值的變量和在程序中給出。但我得到一個錯誤,該變量未定義或不在$ conn語句中使用。

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "sheet"; 
$tbname= $_GET['name']; 

if (isset($_GET['search'])) { 
    try { 
     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     $stmt = $conn->prepare("SELECT * FROM '$tbname'"); 
     $stmt->execute(); 

     $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 

     echo "<tr>"; 
     echo "<th>".$result['Column 1']."</th>"; 
     echo "<th>".$result['Column 2']."</th>"; 
     echo "<th>".$result['Column 3']."</th>"; 
     echo "<th>".$result['Column 4']."</th>"; 
     echo "</tr>"; 
    } 
    catch(PDOException $e) { 
     echo "Error: " . $e->getMessage(); 
    } 
    $conn = null; 
    echo "</table>"; 
} 
+0

試試''SELECT * FROM $ tbname「'你不引用表名。 – miken32

+0

另外,在嘗試將其發送到數據庫之前,您最好檢查一下輸入是否與已知的表名稱匹配。 – miken32

+0

爲什麼你使用get而不是發佈,如果它是從本地html文件到本地php文件? – Gert

回答

0

希望這個代碼將會幫助你,

我稍微改變了你的代碼結構,並添加fetchAll()方法來檢索數據庫中的行,你也可以使用fetch()方法來逐行讀取最後我在最後打印這些行。

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "sheet"; 
$tbname= $_GET['name']; 
$results = array(); 

if (isset($_GET['search'])) { 
    try { 
     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     $stmt = $conn->prepare("SELECT * FROM `$tbname`"); 
     $stmt->execute(); 

     $stmt->setFetchMode(PDO::FETCH_ASSOC); 
     $results = $stmt->fetchAll();//Add this line to get db results 

    } catch(PDOException $e) { 
     echo "Error: " . $e->getMessage(); 
    } 
    $conn = null; 
} 
?> 

<div> 
    <form action="table1.php" method="GET"> 
     <input list="name" name="name"> 
     <datalist id="name"> 
      <option value="table 1"> 
      <option value="table 2"> 
      <option value="table 3"> 
     </datalist> 
     <input type="submit" name="search" value="search"> 
    </form> 
</div> 

<div> 
    <table width="600" border="0" cellpadding="1" cellspacing="1"> 
     <tr> 
      <th>Column 1</th> 
      <th>Column 2</th> 
      <th>Column 3</th> 
      <th>Column 4</th> 
     </tr> 
     <?php 
     if (is_array($results) && count($results)>0) { 
      foreach ($results as $result) { 
       echo "<tr>"; 
       echo "<td>".$result['Column 1']."</td>"; 
       echo "<td>".$result['Column 2']."</td>"; 
       echo "<td>".$result['Column 3']."</td>"; 
       echo "<td>".$result['Column 4']."</td>"; 
       echo "</tr>"; 
      } 
     } 
     ?> 
    </table> 
</div> 
+0

謝謝你Prasad。 –

3

您已將表格名稱放在單引號中,這是不正確的,您需要改爲使用反引號。

在你的PHP代碼行是錯誤的

$stmt = $conn->prepare("SELECT * FROM '$tbname'"); 

應該改變這種

$stmt = $conn->prepare("SELECT * FROM `$tbname`"); 
+0

謝謝Joshi。但是我仍然沒有收到任何迴應,也沒有從數據庫加載數據。 –

+0

當你運行sql查詢時,你會得到任何sql錯誤嗎?請檢查它,並確保數據庫連接設置正確,並且這些表中有數據 –

1

所有你所做的是設定的提取模式,你實際上並沒有取任何東西。

變化:

$stmt->setFetchMode(PDO::FETCH_ASSOC); 

echo "<tr>"; 
echo "<th>".$result['Column 1']."</th>"; 
echo "<th>".$result['Column 2']."</th>"; 
echo "<th>".$result['Column 3']."</th>"; 
echo "<th>".$result['Column 4']."</th>"; 
echo "</tr>"; 

到:

while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) { 
    echo "<tr>"; 
    echo "<th>".$result['Column 1']."</th>"; 
    echo "<th>".$result['Column 2']."</th>"; 
    echo "<th>".$result['Column 3']."</th>"; 
    echo "<th>".$result['Column 4']."</th>"; 
    echo "</tr>"; 
} 
+0

謝謝Augwa,我得到了以下錯誤 解析錯誤:語法錯誤,C:\ wamp64 \ www中的意外'catch'(T_CATCH) \ line \ 74 –

+0

\ sample \ table1.php你省略了已經存在的尾部大括號。在'while(){...}'的大括號之後,你需要有另一個。 – Augwa

+0

正確的代碼格式將有助於防止這樣的問題。我編輯你的問題,以正確格式化您的PHP代碼。 – Augwa

相關問題