Issue with Deleting rows from my second Table in my page

KentjeAtWork

I have a Reports page with 2 tables, one show items in stock the other shows Items sold. I made a delete option for them which provides a button at the right end side of the table to delete rows out of my table. The Issue that I am having is that the Code works perfectly for the 1st table but for the second table, the code will execute but the data does not get deleted from the DB.

I think what is happening is that due to me using the same Code to delete from both tables, that only 1 works. ( I think I am not sure) After looking at it for a while trying to find potential errors I made and trying to see what else might be the issue, I decided to ask u for help!

Here the code:

<?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>

So in short: 1st Table: everything works, all data gets deleted 2nd Table: Appears to be working but after confirming the Delete, the data is still there and didn't get removed from my DB.

The Code for Table 2 is exactly the same as the code for Table 1 exepct for the Names of DB and Table etc.

I am hoping you can go over my code see if you notice anything that might be causing this. Maybe if u agree with what I was thinking, that the same code will not work for both tables on the same page, that you can give an example or a link to how I can tackle this issue? Sorry for the Long code! Thank you in advanced!

Daniel Gimenez

You shouldn't mix the delete's because you might have an instance when one id be the same as the other, so you'll delete the wrong thing. But I don't believe that is your primary problem:

Your select is

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

But your delete is:

DELETE FROM CDE WHERE id = ?";

You're delete should probably be:

DELETE FROM CDE WHERE ID2 = ?";

To Prevent Deleting the wrong thing:

The easiest thing to do here, is change you're delete user JavaScript to accept an action parameter and specify which delete you want to perform, because both delete attempts are running right now.

JavaScript

You don't need the JavaScript twice on the same page. Just have it one time in your HEAD or right before the end of the body.

<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>

Checking Action

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

Rendering Rows

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Issue deleting rows from displayed table

From Dev

fpdf table rows breaks from second page

From Dev

session issue with my page

From Dev

Deleting multiple rows from a table

From Dev

Deleting multiple rows from a table

From Dev

My table is being updated with empty rows upon page reload

From Dev

Session Issue in my Joined Table for my Login

From Dev

i have issue with my database mySQL select from table

From Dev

My SQL - Refer data from another table rows

From Dev

Why is my WHILE loop not reading all the table rows from the database?

From Dev

My SQL - Refer data from another table rows

From Dev

PostgreSQL: deleting rows referenced from another table

From Java

Deleting all rows from Cassandra cql table

From Dev

Deleting rows from 3 tables in MySQL table

From Dev

Tetris Deleting Rows Issue

From Dev

Amazon api not deleting my products from inventory

From Dev

Python DataFrame: Deleting rows by column values that is in my_list

From Dev

My code is not deleting rows, rest all is working fine

From Dev

In Datatables Jquery, makeEditable function, my external jquery library does not working from second page

From Dev

Deleting rows in HTML table

From Dev

MySQL JOIN one of the rows from second table

From Dev

I try to run my "form" but it stuck at the second page with a blank page and did not pass the data to my third page

From Dev

Why is my complete table is locked instead of rows?

From Dev

In my table, colour is appearing only on the even rows

From Dev

Is Kaspersky deleting my files?

From Dev

Logrotate is deleting my logs

From Dev

Preventing Wine from disabling my second monitor

From Dev

How to exit directly from my second activity

From Dev

Download a file from the second HDD of my server

Related Related

  1. 1

    Issue deleting rows from displayed table

  2. 2

    fpdf table rows breaks from second page

  3. 3

    session issue with my page

  4. 4

    Deleting multiple rows from a table

  5. 5

    Deleting multiple rows from a table

  6. 6

    My table is being updated with empty rows upon page reload

  7. 7

    Session Issue in my Joined Table for my Login

  8. 8

    i have issue with my database mySQL select from table

  9. 9

    My SQL - Refer data from another table rows

  10. 10

    Why is my WHILE loop not reading all the table rows from the database?

  11. 11

    My SQL - Refer data from another table rows

  12. 12

    PostgreSQL: deleting rows referenced from another table

  13. 13

    Deleting all rows from Cassandra cql table

  14. 14

    Deleting rows from 3 tables in MySQL table

  15. 15

    Tetris Deleting Rows Issue

  16. 16

    Amazon api not deleting my products from inventory

  17. 17

    Python DataFrame: Deleting rows by column values that is in my_list

  18. 18

    My code is not deleting rows, rest all is working fine

  19. 19

    In Datatables Jquery, makeEditable function, my external jquery library does not working from second page

  20. 20

    Deleting rows in HTML table

  21. 21

    MySQL JOIN one of the rows from second table

  22. 22

    I try to run my "form" but it stuck at the second page with a blank page and did not pass the data to my third page

  23. 23

    Why is my complete table is locked instead of rows?

  24. 24

    In my table, colour is appearing only on the even rows

  25. 25

    Is Kaspersky deleting my files?

  26. 26

    Logrotate is deleting my logs

  27. 27

    Preventing Wine from disabling my second monitor

  28. 28

    How to exit directly from my second activity

  29. 29

    Download a file from the second HDD of my server

HotTag

Archive