从我的页面中的第二个表中删除行的问题

时间:2013-09-11 12:00:09

标签: php mysql pdo

我有一个包含2个表格的报告页面,其中一个显示库存商品,另一个显示已售出的商品。 我为它们做了一个删除选项,它在表的右端提供了一个按钮,用于删除表格中的行。 我遇到的问题是代码对于第一个表是完美的,但对于第二个表,代码将执行但数据不会从数据库中删除。

我认为发生的事情是由于我使用相同的代码从两个表中删除,只有1个有效。 (我想我不确定) 在看了一会儿,试图找到我做的潜在错误并试图看看还有什么问题之后,我决定请求你帮忙!

这里是代码:

<?php


$config['conn'] = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'inventarisdb'
);

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


$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();
    }

}

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

//this is how to get number of rows returned
$num = $stmt->rowCount();



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

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

        //creating our table heading
        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>";

        //retrieve our table contents
        //fetch() is faster than fetchAll()
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            //extract row
            //this will make $row['firstname'] to
            //just $firstname only
            extract($row);

            //creating new table row per record
            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>";


                    //we will use this links on next part of this post
                    echo "<a href='#' onclick='delete_user( {$ID} );'>Delete</a>";
                echo "</td>";
            echo "</tr>";
        }

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

}else{ //if no records found
    echo "No records found.";
}

?>

<script type='text/javascript'>

    function delete_user( id ){
        var answer = confirm('Are you sure?');
        if ( answer ){ //if user clicked ok
            //redirect to url with action as delete and id to the record to be deleted
            window.location = 'Remove.php?action=delete&id=' + id;
        }
    }
</script>

<br/><br/><br/><br/><br/><br/><br/><br/><br/>



<?php


$config['conn'] = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'inventarisdb2'
);

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


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

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

        $query = "DELETE FROM CDE 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();
    }

}

//select all data
$query = "SELECT ID2, Klant, Categorie1, SerieNummer1, MacAdress1, ProductCode1, Prijs1, Hoeveelheid1, Aantekeningen1 FROM CDE";
$stmt = $conn->prepare( $query );
$stmt->execute();

//this is how to get number of rows returned
$num = $stmt->rowCount();



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

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

        //creating our table heading
        echo "<tr>";
            echo "<th>Klant</th>";
            echo "<th>Categorie1</th>";
            echo "<th>SerieNummer1</th>";
            echo "<th>MacAdress1</th>";
            echo "<th>ProductCode1</th>";
            echo "<th>Prijs1</th>";
            echo "<th>Hoeveelheid1</th>";
            echo "<th>Aantekeningen1</th>";
        echo "</tr>";

        //retrieve our table contents
        //fetch() is faster than fetchAll()
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            //extract row
            //this will make $row['firstname'] to
            //just $firstname only
            extract($row);

            //creating new table row per record
            echo "<tr>";
                echo "<td>{$Klant}</td>";
                echo "<td>{$Categorie1}</td>";
                echo "<td>{$SerieNummer1}</td>";
                echo "<td>{$MacAdress1}</td>";
                echo "<td>{$ProductCode1}</td>";
                echo "<td>{$Prijs1}</td>";
                echo "<td>{$Hoeveelheid1}</td>";
                echo "<td>{$Aantekeningen1}</td>";
                echo "<td>";

                    //we will use this links on next part of this post
                    echo "<a href='#' onclick='delete_user( {$ID2} );'>Delete</a>";
                echo "</td>";
            echo "</tr>";
        }

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

}else{ //if no records found
    echo "No records found.";
}

?>

<script type='text/javascript'>

    function delete_user( id ){
        var answer = confirm('Are you sure?');
        if ( answer ){ //if user clicked ok
            //redirect to url with action as delete and id to the record to be deleted
            window.location = 'Remove.php?action=delete&id=' + id;
        }
    }
</script>

简而言之:第一张表:一切正常,所有数据都被删除              第二张表:似乎正在工作但在确认删除后,数据仍然存在,并且没有从我的数据库中删除。

表2的代码与数据库和表等名称的表1 exepct的代码完全相同。

我希望你能查看我的代码,看看你是否注意到可能导致这种情况的任何事情。 也许如果你同意我的想法,相同的代码对同一页面上的两个表都不起作用,你可以给出一个例子或链接来解决这个问题吗? 对不起长码! 先谢谢你了!

1 个答案:

答案 0 :(得分:1)

你不应该混合删除,因为当一个id与另一个id相同时你可能有一个实例,所以你会删除错误的东西。但我不相信这是你的主要问题:

您的选择

SELECT ID2, Klant, Categorie1, SerieNummer1, MacAdress1, ProductCode1, Prijs1, Hoeveelheid1, Aantekeningen1 FROM CDE

但你的删除是:

DELETE FROM CDE WHERE id = ?";

你的删除应该是:

DELETE FROM CDE WHERE ID2 = ?";

防止删除错误的内容:

这里最简单的做法是更改您删除用户JavaScript以接受操作参数并指定要执行的删除操作,因为这两个删除尝试现在都在运行。

<强>的JavaScript

您在同一页面上不需要两次JavaScript。只需在HEAD中或在身体结束前一次。

<script type='text/javascript'>
    function delete_user( action, id ){
        var answer = confirm('Are you sure?');
        if ( answer ){ //if user clicked ok
            //redirect to url with action as delete and id to the record to be deleted
            window.location = 'Remove.php?action=' + action + '&id=' + id;
        }
    }
</script>

检查操作

if ($action=='delete_BCD') { 
// or
if ($action=='delete_CDE') { 

渲染行

echo "<a href='#' onclick='delete_user( \"delete_BCD\", {$ID2} );'>Delete</a>";
// or
echo "<a href='#' onclick='delete_user( \"delete_CDE\", {$ID2} );'>Delete</a>";
相关问题