2015-11-12 94 views
1

我有一個帶有輸入框的html頁面。我試圖從數據庫檢索基於該輸入的數據,並使用php以表格的形式將其發送回原始html頁面。我在一個外部文件中創建了php,它在瀏覽器中顯示了正確的值,但我的問題是試圖在html頁面上顯示php。顯示從數據庫中提取的數據,基於html表單輸入並顯示在html頁面中

我啓用了我的服務器來解析html文件,並將php放在html文件中,但仍然無法正常工作。當我輸入一個值並提交頁面重新加載但沒有顯示。

如果有人知道這樣做的最佳方式,我將不勝感激的意見。我已經閱讀了許多線索,似乎沒有任何工作。

PHP:

<?php 

define('DB_NAME', 'database'); 
define('DB_USER', 'user'); 
define('DB_PASSWORD', 'password'); 
define('DB_HOST', 'localhost'); 

$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
if (!$conn) { 
    die('Could not connect: ' . mysqli_connect_error()); 
    } 

$studentnum = $_POST['studentnum'];  

$sql = "SELECT * FROM test WHERE number LIKE '%$studentnum%'"; 
$result=mysqli_query($conn, $sql); 

echo '<table class="table table-striped table-bordered table-hover">'; 
echo "<tr> 
     <th>Name</th> 
     <th>Number</th> 
     <th>Floor</th> 
     <th>Room</th> 
     <th>Message</th> 
     </tr>"; 
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
{ 
    echo "<tr><td>"; 
    echo $row['name']; 
    echo "</td><td>"; 
    echo $row['number']; 
    echo "</td><td>"; 
    echo $row['floor']; 
    echo "</td><td>"; 
    echo $row['room']; 
    echo "</td><td>"; 
    echo $row['message']; 
    echo "</td></tr>"; 

} 
echo "</table>"; 

mysqli_close($conn); 
?> 

HTML:

<form action="test.php" method="post"> 
      <label>Enter Student Number:</label> 
      <input name="studentnum" type="number" placeholder="Type Here"> 
       <br> 
       <br> 
      <input type="submit" value="Enter"> 

    </form> 
+0

你能查看結果頁面的源代碼嗎? – jerdiggity

回答

2

如果你想這一切在同一頁面上,你可以用下面的,假設你解析PHP在HTML文件中工作,你的頁面index.html(如表單操作中指定的那樣)。

<?php 

// check if the form has been submitted and display the results 
if (isset($_POST['studentnum'])) { 

    define('DB_NAME', 'database'); 
    define('DB_USER', 'user'); 
    define('DB_PASSWORD', 'password'); 
    define('DB_HOST', 'localhost'); 

    $conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
    if (!$conn) { 
    die('Could not connect: ' . mysqli_connect_error()); 
    } 

    // escape the post data to prevent injection attacks 
    $studentnum = mysqli_real_escape_string($conn, $_POST['studentnum']); 

    $sql = "SELECT * FROM `test` WHERE `number` LIKE '%$studentnum%'"; 
    $result=mysqli_query($conn, $sql); 

    // check if the query returned a result 
    if (!$result) { 
     echo 'There are no results for your search'; 
    } else { 
    // result to output the table 
    echo '<table class="table table-striped table-bordered table-hover">'; 
    echo "<tr> 
      <th>Name</th> 
      <th>Number</th> 
      <th>Floor</th> 
      <th>Room</th> 
      <th>Message</th> 
      </tr>"; 
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
    { 
     echo "<tr><td>"; 
     echo $row['name']; 
     echo "</td><td>"; 
     echo $row['number']; 
     echo "</td><td>"; 
     echo $row['floor']; 
     echo "</td><td>"; 
     echo $row['room']; 
     echo "</td><td>"; 
     echo $row['message']; 
     echo "</td></tr>"; 
    } 
    echo "</table>"; 
    } 

    mysqli_close($conn); 
} // end submitted 
else 
{ 
// not submitted to output the form 
?> 
<form action="index.html" method="post"> 
    <label>Enter Student Number:</label> 
    <input name="studentnum" type="number" placeholder="Type Here"> 
    <br> 
    <br> 
    <input type="submit" value="Enter"> 
</form> 
<?php 
} // end not submitted 
?> 
+0

這起作用。非常感謝! –

相關問題