Lisa
Lisa

Reputation: 416

Edit and delete button on each row for table data?

I'm doing a movie library as a school assignment and have list of movies from a database table that I want to have a edit and a delete option next to on each row.

I want to use a edit/delete links for it. Like:

"<a href='moviestorage.php?edit=" . $id . "'>Edit</a>"

But I'm not sure how I can fish up the id for each movie so that it's deleted from the database. What's the query that I should write? Do I need to have a separate delete.php file? I´m a very newbie so bear with me:) Below you can see the code that I've done.

 <?php 

        require 'connect.inc.php';

//This feels incomplete... I´m trying here to fish the ID...
        $id = "SELECT id from movies";
        $query = "DELETE FROM movies WHERE id='$id'";


        $query = "SELECT * FROM movies, categories WHERE movies.genre_id = categories.genre_id";
        $result = mysql_query($query);

        if (!$result) die ("Database access failed:" .mysql_error()) ;

        $rows = mysql_num_rows($result);

        echo '<table><tr><th>Title</th><th>Release year</th><th>Genre</th><th>Director</th><th>Update</th><th>Delete</th></tr>';

        for ($j = 0 ; $j < $rows ; ++$j) {
        echo '<tr><td>' . mysql_result($result,$j,'title') . '</td>' ;
        echo '<td>' . mysql_result($result,$j,'release_year') . '</td>' ;
        echo '<td>' . mysql_result($result,$j,'genre') . '</td>' ;
        echo '<td>' . mysql_result($result,$j,'director') . '</td>' ;
        echo '<td>'."<a href='edit_movie.php?edit=" . $id . "'>Edit</a>".'</td>' ;
        echo '<td>'."<a href='delete.php?delete=" . $id . "'>Delete</a>".'</td></tr>' ;

        }
        echo '</table>'; 

        include 'add_movie.php';
    ?>

Upvotes: 1

Views: 29995

Answers (3)

anu g prem
anu g prem

Reputation: 560

If you need a simple and effective solution, use datatables. :) please search in google. spent some time to study how to implement it, it will save a lot of time in future.

Upvotes: 0

dp4solve
dp4solve

Reputation: 411

if you have to edit and delete on new page than you can put hyperlink this way

  echo "<a href=\"edit.php?id=".mysql_result($result,$j,'id')."\"><strong>EDIT</strong>";

  echo "<a href=\"delete.php?id=".mysql_result($result,$j,'id')."\"><strong>delete</strong>";

and for conformation for delete you can use this on delete link

  echo "<a href=\"delete.php?id=".mysql_result($result,$j,'id')."\" onclick=\"return confirm('You want to delete your own account???');\"><strong>delete</strong>";

than get the id on next page using

    $id = $_GET['id'];

hope it will be usefull for you

Upvotes: 0

jeroen
jeroen

Reputation: 91734

I would recommend that you use a form for every row with a delete and edit button and a hidden field with the ID. You can then post that form to the right script and determine the action to take there.

If you have to use a link to delete an item, at least have the link lead to another confirmation page with a form that the user has to submit and that posts to your delete script.

Upvotes: 1

Related Questions