【问题标题】:Two tables displayed on one page using PHP, PDO and MySql使用 PHP、PDO 和 MySql 在一个页面上显示两个表
【发布时间】:2017-09-26 11:45:49
【问题描述】:

我正在尝试创建一个 CRUD 仪表板,使用 MySql 后端和带有 PHP 和 PDO 的引导前端与数据库进行通信。我是 Web 开发的菜鸟,但不是编码的菜鸟。

我们的目标是创建一个网络应用程序来记录我的患者咨询。因此,我的表结构是一个“主”表和两个与“主”表有关系的子表,名为“consults”和“procedures”。

我正在尝试制作一个仪表板,在其中显示我的“主”表,然后在其下方添加两个子表。 (稍后我会更好地设计它,但我正在努力让它发挥作用)。

以下是我能想到的最好的 MWE(如果有人有更简单的解决方案,我会喜欢它)。第一个“日志患者”表运行良好,并且可以很好地显示患者行。第二个表是问题所在,特别是:

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

这是我不断收到错误的区域。错误是:

致命错误:未捕获的错误:在 /home/paincl5/public_html/logbook/logbook.php:110 中调用 null 时的成员函数 query() 堆栈跟踪:在 /home/paincl5/public_html 中抛出 #0 {main} /logbook/logbook.php 第 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>

【问题讨论】:

    标签: php mysql twitter-bootstrap pdo crud


    【解决方案1】:

    您正在使用unset($pdo) 破坏您的$pdo 变量,因此任何后续调用都试图针对空对象运行方法。

    尝试删除对 unset 的引用。

    我假设$pdo 对象来自config.php。由于您使用的是require_once,它只会在第一次调用require_once 时包含配置文件。这就是$pdo 被销毁而不是重新创建的原因。

    【讨论】:

    • 完美,我只在第三个表中留下了未设置的引用(上面未显示),它修复了它。很棒。
    猜你喜欢
    • 1970-01-01
    • 2014-01-02
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多