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

时间:2013-09-11 06:38:50

标签: php pdo

我正在使用此代码,它允许我在表格中查看我的数据库记录,在此表格中有一个删除或编辑记录的选项。 只有我收到此错误消息,我无法弄清楚我做错了什么。

错误消息:

致命错误:在第32行的C:\ wamp \ www \ Inventaris ++ \ NinjaCodeDelete.php中的非对象上调用成员函数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>

我还想说我不是高级程序员,我在网上找到了这个代码,它似乎适用于其他使用它的人。 我有一些Mysql和PHP的经验,但没有PDO的经验。 我希望你能帮助我! 先谢谢你。

1 个答案:

答案 0 :(得分:-1)

include'Connect2db3.php';”中有什么内容?

有了这个,我指的是连接2db3.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']);

?>