2

私は 2 つのテーブルを含むレポート ページを持っています。テーブルの右端にあるボタンを使用して、テーブルから行を削除するための削除オプションを作成しました。私が抱えている問題は、コードが最初のテーブルでは完全に機能することですが、2 番目のテーブルではコードは実行されますが、データは DB から削除されません。

何が起こっているのかと思うと、同じコードを使用して両方のテーブルから削除しているため、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>

要するに: 1 番目のテーブル: すべてが機能し、すべてのデータが削除されます 2 番目のテーブル: 動作しているように見えますが、削除を確認した後、データはまだそこにあり、DB から削除されませんでした。

表 2 のコードは、DB やテーブルなどの名前を除いて、表 1 のコードとまったく同じです。

私のコードを調べて、これを引き起こしている可能性のあるものに気付いたかどうかを確認していただければ幸いです。同じページの両方のテーブルで同じコードが機能しないという私の考えに同意する場合は、この問題に取り組む方法の例またはリンクを教えてください。コードが長くてすみません!よろしくお願いします!

4

1 に答える 1