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 12 of 12
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Delete mysql row in table

    Table is something like this:

    PHP Code:
    <table class="center">
    <tr>
    <th>Name</th>
    <th>Date</th>
    </tr>

    <?php

    if(isset($_POST["Delete"])) {
    $sql sprintf("DELETE FROM names WHERE name = '".$name."'");
    $result mysql_query($sql);
    }

    echo 
    "</td><td>";
    echo 
    $name;
    echo 
    "</td><td>";
    echo 
    $date;
    echo 
    "</td><td>";
    echo 
    '<form name="form1" method="post" action="'.$_SERVER['PHP_SELF'].'">
    <input type="image" src="ima/delete.png" name="Delete" id="submit"></form>'
    ;
    echo 
    "</td></tr>";
    echo 
    "</table></div>";
    ?>
    Each name behind is the delete.png button and if i click this then delete only this name row in table.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,538
    Thanks
    45
    Thanked 259 Times in 256 Posts
    So... what's the problem?

    And that's bad DB design; what if two people have the same name?

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    And WHERE is your $name value coming from?

    In general, that code makes no sense at all.

    You can only delete one name at a time, so the delete query must occur ONLY ONCE and should happen *BEFORE* you list all the names.

    Possible example:
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <?php
    ... create your connection to the DB here ...
    
    $delete = $_POST["Delete"];
    
    if( isset( $delete ) ) 
    { 
        $delete = mysql_real_escape_string($delete);
        $sql ="DELETE FROM names WHERE name = '$delete'";
        mysql_query($sql) or die("Delete of $delete failed: " . mysql_error() );
    } 
    
    $sql = "SELECT name, date FROM names ORDER BY name";
    $result = mysql_query($sql) or die("SELECT failed: " . mysql_error() );
    ?>
    <table class="center"> 
    <tr>
        <th>Name</th> 
        <th>Date</th> 
        <th>click to delete</th>
    </tr> 
    <?php 
    while ( $row = mysql_fetch_assoc($result) ) 
    {
        $name = $row["name"];
        $date = $row["date"];
        echo "<tr><td>$name</td><td>$date</td><td><form method=\"post\">";
        echo "<input type=\"hidden\" name=\"delete\" value=\"$name\" />";
        echo '<input type="image" src="ima/delete.png"></form>'; 
        echo "</td></tr>\n"; 
    }
    ?>
    </table>
    </body>
    </html>
    Note that you DO NOT NEED (or want) a name or id on the <input type="image">.

    Instead, you want a hidden field to hold the $name that you want to use to specify what is to be deleted.
    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.

  • #4
    New Coder
    Join Date
    May 2012
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    my version dosen't work and cant be same name

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    And Keleth is correct: What if two people have the same name?

    And finally, you are STILL using the OBSOLETE mysql library. You *MUST* move to using mysqli !!
    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.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    Quote Originally Posted by Radeom View Post
    my version dosen't work and cant be same name
    Okay, if it can't be same name.

    But your version would never work, because you were never telling *WHICH* name to delete in your <form>!!!
    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.

  • #7
    New Coder
    Join Date
    May 2012
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks Old Pedant now work :P
    Only the row delete in table after i refresh or click again button

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,538
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by Old Pedant View Post
    Okay, if it can't be same name.

    But your version would never work, because you were never telling *WHICH* name to delete in your <form>!!!
    Given the context of the file, presumably the name is passed as a URL parameter, which is problematic.

  • #9
    New Coder
    Join Date
    May 2012
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok i insert
    header("location:namestable.php");

    and now work good

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    I don't use PHP, so no idea why you needed that header. But glad it was as simple to fix as that.
    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.

  • #11
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,538
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by Old Pedant View Post
    I don't use PHP, so no idea why you needed that header. But glad it was as simple to fix as that.
    I do use PHP and I have no idea why he needs the header.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    Yeah, I don't get it, now that I read up on what header("location:...") means. Why would you want to redirect before you ever got a chance to run the code on the page???
    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.


  •  

    Posting Permissions

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