Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Allow user to edit and delete row

    I've created a table with movies and a webpage where you can add more and view all of them. Now I'm trying to add if statements for if the edit or delete link has been clicked. Here is the code so far:

    PHP Code:
    <?php // movielibrary.php
    require_once 'login.php';
    $query "SELECT * FROM library";

    $result mysql_query($query);
    if (!
    $result) die ("Database access failed: " mysql_error());
    $rows mysql_num_rows($result);

    echo 
    "<table border='1'><tr> <th>Title</th>
            <th>Director</th>
        <th>Year</th><th>Category</th>
            <th>Update</th><th>Delete</th></tr>"
    ;

    for (
    $j $j $rows ; ++$j)
    {
        
    $row mysql_fetch_row($result);
        echo 
    "<tr><td>$row[1]</td><td>$row[2]</td> 
            <td>$row[3]</td><td>$row[4]</td>
        <td><a href='movielibrary.php?edit=$row[0]'>Edit</a</td>
            <td><a href='movielibrary.php?delete=$row[0]'>Delete</a></td></tr>"
    ;
    }

    echo 
    "</table>";

    mysql_close($db_server);
    ?>
    So my question is: how could I test the condition of if one of the links has been clicked? Thanks beforehand!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,506
    Thanks
    77
    Thanked 4,378 Times in 4,343 Posts
    I don't think you should try to do edit in the same page. Editing a record is so different it deserves its own page. So I would change
    Code:
    <a href='movielibrary.php?edit=$row[0]'>Edit</a>
    to
    Code:
    <a href='movieedit.php?id=$row[0]'>Edit</a>
    or something along those lines.

    Delete, though, is easy:

    I don't use PHP, but something like this:
    Code:
    <?php // movielibrary.php 
    require_once 'login.php'; 
    
    $deleteWhich = $_GET["delete"];
    if ( isset($deleteWhich ) ) 
    {
        $sql = "DELETE FROM library WHERE id = " . ( (int) $deleteWhich );
        mysql_query( $sql ) or die( mysql_error() );
    }
    
    ... rest of page same ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Wilhelmina (10-10-2012)

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you Old Pedant for your reply!

    I would prefer having the edit on the same page though because once clicked the form for adding a new movie will change to a replicate of the form but with the details of the movie clicked to edit already in place. Could i not use the action attribute in the form to a seperate script where the UPDATE will take place? (Sorry, as you might have noticed I'm quite new to this.) Here is what the code looks like now:

    PHP Code:
    <?php // movielibrary.php
    require_once 'login.php';
    $query "SELECT * FROM library";
    $result mysql_query($query);
    if (!
    $result) die ("Database access failed: " mysql_error());
    $rows mysql_num_rows($result);

    echo 
    "<table border='1'><tr> <th>Title</th><th>Director</th>
        <th>Year</th><th>Category</th><th>Update</th><th>Delete</th></tr>"
    ;

    for (
    $j $j $rows ; ++$j)
    {
        
    $row mysql_fetch_row($result);
        echo 
    "<tr><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td>
        <td><a href='movielibrary.php?edit=$row[0]'>Edit</a</td><td><a href='movielibrary.php?delete=$row[0]'>Delete</a></td></tr>"
    ;
    }

    echo 
    "</table>";

    if(isset(
    $_GET['edit']))
    {
    echo <<<_END
    <form action="update.php" method="post">
    Choose a category:<br />
    <input type="hidden" name="id" value="$row
    [0]" />
    <input type="radio" name="category" value="$row
    [4]" />Thriller<br />
    <input type="radio" name="category" value="$row
    [4]" />Romantic<br />
    <input type="radio" name="category" value="$row
    [4]" />Swedish<br />
    <input type="radio" name="category" value="$row
    [4]" />Animated<br />
    <input type="radio" name="category" value="$row
    [4]" />Comedy<br /><br />
       Title <input type="text" name="title" value="$row
    [1]" /><br />
    Director <input type="text" name="director" value="$row
    [2]" /><br />
        Year <input type="text" name="year" value="$row
    [3]" /><br />
             <input type="submit" value="CHANGE" />
    </form>
    _END;
    }

    elseif(isset(
    $_GET['delete'])){
       
    $delete $_GET['delete'];
       
    $query mysql_query("DELETE FROM library WHERE id='$row[0]'")or die(mysql_error());
       
    header("Location: movielibrary.php"); 
    }

    else
    {
    echo <<<_END
    <form action="store.php" method="post">
    Choose a category: <br />
    <input type="hidden" name="id" />
    <input type="radio" name="category" value="Thriller" />Thriller<br />
    <input type="radio" name="category" value="Romantic" />Romantic<br />
    <input type="radio" name="category" value="Swedish" />Swedish<br />
    <input type="radio" name="category" value="Animated" />Animated<br />
    <input type="radio" name="category" value="Comedy" />Comedy<br /><br />
       Title: <input type="text" name="title" /><br />
    Director: <input type="text" name="director" /><br />
        Year: <input type="text" name="year" /><br />
             <input type="submit" value="STORE" />
    </form>
    _END;
    }

    mysql_close($db_server);
    ?>
    How can I get the values from the row with the id in the edit link to show in the edit form?

    Thanks beforehand,
    Wilhelmina

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,506
    Thanks
    77
    Thanked 4,378 Times in 4,343 Posts
    Of course you can put it on the same page. Just complicates things a little bit.

    But you need to do a *SEPARATE* query to get the data for the edit.

    Just as I put the query for the DELETE *before* the SELECT * so you would put the query for the edit before the main query and main display. (Or after the main display, if you prefer.)

    Your code WILL NOT WORK as is. Because you do the DELETE *after* you do the SELECT * you will get *ALL* the records *AGAIN*. And then, after displaying them all, you do the DELETE. Too late to change the display.

    The DELETE *must* be the first code on the page. The EDIT can be either second or last.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •