0

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

错误信息:

致命错误:在第 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 没有。我希望你能帮助我!谢谢你。

4

1 回答 1

-1

你的“ include'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']);

?>
于 2013-09-11T08:12:57.970 回答