Search code examples
phppostgresqlsql-delete

2 DELETE, only one working [POSTGRESQL]


I'm literaly going crazy over this one. So I have my gamePage.php file where I can see all the info about a single videogame. The button delete invokes the following code

    if(isset($_POST['delete'])){
        $query="DELETE FROM shop.videogame WHERE IDVideogame='$id'";
        pg_query($conn, $query);
        $query="DELETE FROM shop.storage WHERE videogame='$id'";
        pg_query($conn,$query);
        echo 'Game deleted, click the "Go Back" button';            
     }

What actually happens is that only the second Delete works. The videogame is deleted from the storage table, but not from the videogame table. What's even stranger is that if I click a second time the button, the first DELETE finally works.

In other words one is supposed to click for 2 times the same button for no reason.


Solution

  • I guess shop.storage.videogame is a ForeignKey to shop.videogame.IDVideogame.

    If I have right, then switch delete statements.

    $query="DELETE FROM shop.storage WHERE videogame='$id'";
    pg_query($conn, $query);
    $query="DELETE FROM shop.videogame WHERE IDVideogame='$id'";
    pg_query($conn, $query);