调用函数有时不起作用

时间:2017-08-15 22:17:29

标签: php database user-defined-functions

在我正在进行的项目中,我希望从类中的函数中获取数据。因此我从我的主索引文件中调用该函数。但是,在我最初想要拨打电话的地方,我收到以下错误:

  

致命错误:未捕获错误:调用成员函数searchKlant()   在N:\ xampp \ htdocs \ Sylvia project \ index.php中的数组:66堆栈跟踪:

     在第66行的N:\ xampp \ htdocs \ Sylvia project \ index.php中抛出

0 {main}

但是,如果我使用相同的代码行:

$klantfor = $klant->searchKlant($_GET['k']);

在相同代码的不同位置,我确实得到了所需的结果。 如果我调用相同的函数会让它变得更有趣,但这次它在不同的类中确实有效。

如果有人能帮助我解决问题,我将不胜感激。 提前谢谢

主页面代码:

<?php
spl_autoload_register(function ($class_name) {
    include 'custom/' . $class_name . '.class.php';
});

$klant = new Klant;
$invoice = new Invoice;

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['form'])){
    if($_POST['form']=="klant") {
       $klant->addKlant($_POST);
    } else if($_POST['form']=="invoice"){
       $invoice->addInvoice($_POST);
    }
}

$allklanten = $klant->getKlanten();
$allinvoice = $invoice->getInvoice();


?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title></title>
    <meta charset="UTF-8">
</head>
<body>
    <?php include("components/header.php") ?>

    <div class="row">

        <div class="col-md-2">
            <ul class="col-sm-4 list-unstyled">
                <?php
                    foreach($allklanten as $klant){
                        echo "<li><a href='?page=0&k=" . $klant['kID'] . "'>" . $klant['kVoornaam'] . " " . $klant['kAchter'] . "</a></li><br>";
                    }
                ?>
            </ul>
        </div>

        <?php
            if(isset($_GET['page'])) {
                if ($_GET['page'] == 2) {
                    include("pages/klantadd.php");
                } else if ($_GET['page'] == 3) {
                    include("pages/invoiceadd.php");
                }
            }
        ?>

        <div class="col-md-8">
            <?php
                if(isset($_GET['k'])){

                    $klantfor = $klant->searchKlant($_GET['k']);
                    $invoicefor = $invoice->searchInvoice($_GET['k']);

                    foreach ($klantfor as $klant){
                    echo "<h3>" . $klant['kVoornaam'] . " " . $klant['kAchter'] . "</h3>";}

                    echo "<table class=\"table table-hover\">
                            <thead>
                              <tr>
                                <th>ID</th>
                                <th>Datum</th>
                                <th>test.class</th>
                              </tr>
                            </thead>
                            <tbody>
                          ";

                    foreach($invoicefor as $invoice){
                        echo "<tr>";
                        echo "<td>" . $invoice['iID'] . "</td>";
                        echo "<td>" . $invoice['iDate'] . "</td>";
                        echo "</tr>";
                    }

                    echo "</tbody></table>";
                }
            ?>
        </div>

    </div>
</body>

有问题的功能:

public function searchKlant($target){
        $db = new Database();
        $db = $db->getConn();

        $statement = $db->prepare("SELECT * FROM klant WHERE kID = ?");
        $statement->bind_param("i",$target);
        $statement->execute();
        $result = $statement->get_result();
        while ($row = $result->fetch_assoc()) {
            $klant[] = $row;
        }
        $statement->close();
        $db->close();

        return $klant;
    }

1 个答案:

答案 0 :(得分:3)

您在此处覆盖了$klant课程的实例Klant

foreach($allklanten as $klant) {....

所以将其更改为其他内容($ klantItem左右)。

见这个简单的例子:

<?php

$array = Array(1,2,3);

$v="a class";
foreach($array as $v) {
    echo $v;
}

echo "outside: ".$v;
// output: 'outside: 3' - so "a class" is overwritten.