Php 使用PDO删除记录时出现未知错误

Php 使用PDO删除记录时出现未知错误,php,pdo,Php,Pdo,我使用的代码允许我在一个表中查看我的DB记录,在这个表中有一个删除或编辑记录的选项。 只有我收到了这个错误信息,我不知道我做错了什么 错误消息: 致命错误:对C:\wamp\www\Inventaris++\NinjaCodeDelete.php第32行的非对象调用成员函数prepare() 守则: <?php include'Connect2db3.php'; $action = isset($_GET['action']) ? $_GET['action']: ""; if($ac

我使用的代码允许我在一个表中查看我的DB记录,在这个表中有一个删除或编辑记录的选项。 只有我收到了这个错误信息,我不知道我做错了什么

错误消息:

致命错误:对C:\wamp\www\Inventaris++\NinjaCodeDelete.php第32行的非对象调用成员函数prepare()

守则:

<?php
include'Connect2db3.php';

$action = isset($_GET['action']) ? $_GET['action']: "";

if($action=='delete'){ //if the user clicked ok, run our delete query
    try {

        $query = "DELETE FROM BCD WHERE id = ?";
        $stmt = $conn->prepare($query);
        $stmt->bindParam(1, $_GET['id']);

        $result = $stmt->execute();
        echo "<div>Record was deleted.</div>";

    }catch(PDOException $exception){ //to handle error
        echo "Error: " . $exception->getMessage();
    }

}

$query = "SELECT ID, Categorie, SerieNummer, MacAdress, ProductCode, Prijs, RekNummer, PaletNummer, Hoeveelheid, Aantekeningen FROM BCD";
$stmt = $conn->prepare( $query );
$stmt->execute();

$num = $stmt->rowCount();

echo "<a href='reports.php'>View Reports</a>";

if($num>0){ //check if more than 0 record found

    echo "<table border='1'>";//start table

        echo "<tr>";
            echo "<th>Categorie</th>";
            echo "<th>SerieNummer</th>";
            echo "<th>MacAdress</th>";
            echo "<th>ProductCode</th>";
            echo "<th>Prijs</th>";
            echo "<th>RekNummer</th>";
            echo "<th>PaletNummer</th>";
            echo "<th>Hoeveelheid</th>";
            echo "<th>Aantekeningen</th>";
        echo "</tr>";

        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){

            extract($row);

            echo "<tr>";
                echo "<td>{$Categorie}</td>";
                echo "<td>{$SerieNummer}</td>";
                echo "<td>{$MacAdress}</td>";
                echo "<td>{$ProductCode}</td>";
                echo "<td>{$Prijs}</td>";
                echo "<td>{$RekNummer}</td>";
                echo "<td>{$PaletNummer}</td>";
                echo "<td>{$Hoeveelheid}</td>";
                echo "<td>{$Aantekeningen}</td>";
                echo "<td>";

                    echo "<a href='edit.php?id={$id}'>Edit</a>";
                    echo " / ";

                    echo "<a href='#' onclick='delete_user( {$id} );'>Delete</a>";
                echo "</td>";
            echo "</tr>";
        }

    echo "</table>";//end table

}else{ 
    echo "No records found.";
}

?>

<script type='text/javascript'>

    function delete_user( id ){
        var answer = confirm('Are you sure?');
        if ( answer ){ 
            window.location = 'NinjaCodeDelete.php?action=delete&id=' + id;
        }
    }
</script>
你的“
包括'Connect2db3.php';
”里面是什么

我指的是connect2db3.php,这个文件在正确的文件夹中吗

您的连接可能如下所示:

<?php
$config['conn'] = array(
'host' => 'yourHostName',
'username' => 'yourUserName',
'password' => 'yourPassword',
'dbname' => 'yourDBName'
);

$conn = new PDO('mysql:host=' . $config['conn']['host'] . ';dbname=' . $config['conn']['dbname'], $config['conn']['username'], $config['conn']['password']);

?>


如何初始化$conn
您还没有初始化$conn。要访问成员函数,您需要创建该类的对象。为什么将此错误称为“未知”?我在这个链接中看到了不同的类。但是初始化和创建该类的对象意味着什么呢?你能举个例子吗?你真的不知道创建类的对象是什么意思?先去学吧。这是最基本的东西之一。它很有效!:)谢谢你,猎户座!:)