2017-09-26 79 views
0

我試圖創建一個CRUD儀表板,使用MySql後端和引導前端用PHP和PDO與數據庫進行通信。我是網頁開發的一個小老頭,但不是編碼。使用PHP,PDO和MySql顯示在一個頁面上的兩個表格

目標是創建一個Web應用程序來記錄我的患者諮詢。因此,我的表結構是一個單獨的「主」表和兩個與「主」表關聯的子表,名爲「諮詢」和「過程」。

我想做一個儀表板,在那裏顯示我的「主」表,然後在它下面添加兩個子表。 (稍後我會更好地設計風格,但我試圖讓它工作)。

以下是我能想到的最好的MWE(如果有人有一個更簡單的解決方案,會喜歡它)。第一個「日誌病人」表運作良好,並能很好地顯示病人行。它的第二個表是問題,特別是:

$sql = "SELECT * FROM proc"; 
if($result = $pdo->query($sql)){ 
if($result->rowCount() > 0){ 

這是我不斷收到錯誤的區域。錯誤是:

Fatal error: Uncaught Error: Call to a member function query() on null in /home/paincl5/public_html/logbook/logbook.php:110 Stack trace: #0 {main} thrown in /home/paincl5/public_html/logbook/logbook.php on line 110

在管線110中的代碼是

unset($pdo); 

我完整的代碼:

<div class="wrapper"> 
    <div class="container-fluid"> 
     <div class="row"> 
      <div class="col-md-12"> 
       <div class="page-header clearfix"> 
        <h2 class="pull-left">Logbook Patients</h2> 
        <a href="create.php" class="btn btn-success pull-right" >Add New Patient</a> 


       </div> 
       <?php 
       // Include config file 
       require_once 'config.php'; 

       // Attempt select query execution 
       $sql = "SELECT * FROM main"; 
       if($result = $pdo->query($sql)){ 
        if($result->rowCount() > 0){ 
         echo "<div style='height:300px;overflow-y:scroll;;'>"; 
         echo "<table class='table table-bordered table-striped'>"; 
          echo "<thead>"; 
           echo "<tr>"; 
            echo "<th>Surname</th>"; 
            echo "<th>first_name</th>"; 
            echo "<th>DOB</th>"; 
            echo "<th>Hospital</th>"; 
            echo "<th>MRN</th>"; 
            echo "<th>Action</th>"; 
           echo "</tr>"; 
          echo "</thead>"; 
          echo "<tbody>"; 
          while($row = $result->fetch()){ 
           echo "<tr>"; 
            echo "<td>" . $row['Surname'] . "</td>"; 
            echo "<td>" . $row['first_name'] . "</td>"; 
            echo "<td>" . $row['DOB'] . "</td>"; 
            echo "<td>" . $row['Hospital'] . "</td>"; 
            echo "<td>" . $row['MRN'] . "</td>"; 
            echo "<td>"; 
             echo "<a href='read.php?id=". $row['id'] ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>"; 
             echo "<a href='update.php?id=". $row['id'] ."' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>"; 
             echo "<a href='delete.php?id=". $row['id'] ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>"; 
            echo "</td>"; 
           echo "</tr>"; 
          } 
          echo "</tbody>";        
         echo "</table>"; 
         echo "</div>"; 
         // Free result set 
         unset($result); 
        } else{ 
         echo "<p class='lead'><em>No records were found.</em></p>"; 
        } 
       } else{ 
        echo "ERROR: Could not able to execute $sql. " . $mysqli->error; 
       } 

       // Close connection 
       unset($pdo); 
       ?> 
      </div> 
     </div>   
    </div> 
</div> 



// Procedure Table 
<div class="wrapper"> 
    <div class="container-fluid"> 
     <div class="row"> 
      <div class="col-md-12"> 
       <div class="page-header clearfix"> 
        <h2 class="pull-left">Procedures</h2> 
        <a href="create_proc.php" class="btn btn-success pull-right" >Add New Procedure</a> 
       </div> 
       <?php 
       // Include config file 
       require_once 'config.php'; 
       // Attempt select query execution 
       $sql = "SELECT * FROM proc"; 
       if($result = $pdo->query($sql)){ 
        if($result->rowCount() > 0){ 
         echo "<div style='height:300px;overflow-y:scroll;;'>"; 
         echo "<table class='table table-bordered table-striped'>"; 
          echo "<thead>"; 
           echo "<tr>"; 
            echo "<th>Procedure Type</th>"; 
            echo "<th>Procedure Name</th>"; 
            echo "<th>Notes</th>"; 
            echo "<th>Action</th>"; 
           echo "</tr>"; 
          echo "</thead>"; 
          echo "<tbody>"; 
          while($row = $result->fetch()){ 
           echo "<tr>"; 
            echo "<td>" . $row['procedure_type'] . "</td>"; 
            echo "<td>" . $row['procedure_name'] . "</td>"; 
            echo "<td>" . $row['notes'] . "</td>"; 
            echo "<td>"; 
             echo "<a href='update.php?id=". $row['id1'] ."' title='Update Record' data-toggle='modal' data-target='#myModal' ><span class='glyphicon glyphicon-pencil'></span></a>"; 
             echo "<a href='delete.php?id=". $row['id1'] ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>"; 
            echo "</td>"; 
           echo "</tr>"; 
          } 
          echo "</tbody>";        
         echo "</table>"; 
         echo "</div>"; 
         // Free result set 
         unset($result); 
        } else{ 
         echo "<p class='lead'><em>No records were found.</em></p>"; 
        } 
       } else{ 
        echo "ERROR: Could not able to execute $sql. " . $mysqli->error; 
       } 

       // Close connection 
       unset($pdo); //Here occurs the error (line 110) 
       ?> 
      </div> 
     </div>   
    </div> 
</div> 

回答

0

您與unset($pdo)所以任何後續調用試圖摧毀你的$pdo變量針對空對象運行方法。

嘗試刪除對該參考文獻的引用unset

我假設$pdo對象來自config.php。由於您使用的是require_once,因此只會在第一次調用require_once時包含配置文件。這就是爲什麼$pdo被銷燬並且沒有被重新創建。

+0

完美的是,我只在第三個表中留下了未設置的引用(未在上面顯示),並修復了它。優秀。 – user2444133

相關問題