0

My aim is to delete a row whenever I click delete button. My method doing this is by passing the value of ['movies_id'] and storing it inside a hidden input and then passing it when delete button is pressed. The issue is that when I press the delete button it only reads the first id, which in this case is 1. Even though I press the delete button in the 3 card for example. So, my question is how do I pass the correct 'movies_id' stored inside 'keyToDelete' so that it doesn't always read 1?

As you can see in the image below, the id from mysql is being read well inside the card.

function displayMovies()
{
    global $dbc;
    $movieSelect = "SELECT * FROM movies_tbl";
    $query = mysqli_query($dbc, $movieSelect);

    if (mysqli_num_rows($query) == 0) {

        echo "There is nothing to display.";
    } else {
        $edit = "";
        $delete = "";
        if (isset($_SESSION['type'])) {
            if ($_SESSION['type'] == "admin") {
                $edit = "<a href='#' class='btn btn-cyan align-self-end'>Edit</a>";
                $delete = '<input type = "submit" name="delete" value="Delete" form="movieForm" class="btn btn-cyan align-self-end">';
                while ($row = mysqli_fetch_assoc($query)) {
                    movieDesc($row['movies_id'], $row['movie_title'], $row['main_actor'], $row['movie_length'], $row['average_rating'], $row['release_date'], $row['description'], $row['img_path'], $row['trailer_url'], $edit, $delete);
                }
            } else if ($_SESSION['type'] == "user") {
                while ($row = mysqli_fetch_assoc($query)) {
                    movieDesc($row['movies_id'],$row['movie_title'], $row['main_actor'], $row['movie_length'], $row['average_rating'], $row['release_date'], $row['description'], $row['img_path'], $row['trailer_url'], $edit, $delete);
                }
            }
        } else {
            for ($i = 1; $i < 4; $i++) {
                $row = mysqli_fetch_assoc($query);
                movieDesc($row['movies_id'],$row['movie_title'], $row['main_actor'], $row['movie_length'], $row['average_rating'], $row['release_date'], $row['description'], $row['img_path'], $row['trailer_url'], $edit, $delete);
            }
        }
    }
}

if (isset($_POST['delete'])){
    $key = $_POST['keyToDelete'];
    echo"<h1>$key</h1>";
}

function movieDesc($movies_id,$movie_title, $main_actor, $movie_length, $average_rating, $release_date, $description, $img_path, $trailer_url, $edit, $delete)
{
    echo '<div class="col-md-4 mb-4">
            <div class="card">
                <img src="images/' . $img_path . '.jpg" class="card-img-top cardImage">
                <div class="card-body d-flex flex-column">
                <form action="movies.php" id="movieForm" method="post">
                    <h3 class="card-title text-center fontEDO">' . $movie_title, $movies_id . '</h3>
                    <div class="row">
                        <div class="col">
                            <p class="card-text text-left"><b>Main Actor:</b></p>
                            <p class="card-text text-left"><b>Movie Length:</b></p>
                            <p class="card-text text-left"><b>Average Rating:</b></p>
                            <p class="card-text text-left"><b>Release Date:</b></p>
                        </div>
                        <div class="col">
                            <p class="card-text text-left">' . $main_actor . '</p>
                            <p class="card-text text-left">' . $movie_length . '</p>
                            <p class="card-text text-left">' . $average_rating . '</p>
                            <p class="card-text text-left">' . $release_date . '</p>
                        </div>
                    </div>
                    <p class="card-text text-left description"><br><b>Description:</b> ' . $description . '</p>
                    <input type="text" name="keyToDelete" value='. $movies_id .'>
                    </form>
                    <div class="mt-auto text-center">
                    <a href = "' . $trailer_url . '" target="_blank" class="btn btn-cyan align-self-end">Watch Trailer</a>
                        ' . $edit . '
                        ' . $delete . '
                    </div>
                </div>
            </div>
        </div>';
}

enter image description here

  • 1
    Where is the hidden input? – Barmar Jun 07 '21 at 19:28
  • What is the value of `$delete`? – Barmar Jun 07 '21 at 19:32
  • The input field is not hidden yet for debugging purpose. So what would be my best way to fix this? Some how make them all in 1 form? Thanks (Also added the value of delete in edit) – Janssen Schembri Jun 07 '21 at 19:32
  • 1
    The Edit and Delete buttons need to be inside the form. – Barmar Jun 07 '21 at 19:34
  • Having the edit and delete buttons inside the form doesn't seem to have any affect. – Janssen Schembri Jun 07 '21 at 19:38
  • This code is fairly confusing in how it produces its output. What is the actual resulting HTML that it generates? Within that HTML, can you identify how you're expecting the form post to be structured? – David Jun 07 '21 at 19:39
  • @David the resulting HTML is what it shows in the image. The reason why I have multiple movieDesc functions is because I'm using sessions and don't want to give users the same permissions as admins. – Janssen Schembri Jun 07 '21 at 19:43
  • Just as an FYI, anytime you are echoing out big chunks of HTML, you should probably rethink things. Ideally you'd have your HTML in a separate file completely, just breaking into PHP for echoing variable values and some simple loops or conditional statements. This can be a full-fledged templating engine, or just something as simple as pulling in a file with `include`. – miken32 Jun 07 '21 at 20:45

1 Answers1

3

You can't have duplicate id="movieForm". When you use form="movieForm" in the submit button, it submits the first form with that ID, not the one just before the button.

You should move the submit button inside the form, and get rid of form="movieForm" from the button.

Or give each form a unique ID, and use that in the form attribute of the submit button.

Barmar
  • 741,623
  • 53
  • 500
  • 612