www.webdeveloper.com
Results 1 to 2 of 2

Thread: SQL Form remove 1 single row

  1. #1
    Join Date
    Jul 2011
    Location
    Haarlem
    Posts
    30

    SQL Form remove 1 single row

    I'm trying to create a form so that the user can click on the checkbox to remove the selected link from the sql database, however what I ever try to do it just doesn't seem to work and I guess I'm in the wrong position of get it correctly to work.

    Code:
    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <?php
    include("../../database/sqlconnect.php");
    
    $query=mysql_query("SELECT * FROM leftmenulink");
    
    while($query2=mysql_fetch_array($query))
    {
    echo $query2['links'];
    echo '<input type="checkbox" name="links"> <br>';
    }
    ?>
    <input type="submit" value="remove" name="remove">
    </form>
    
    <?php
    if(isset($_POST["remove"]) && isset($_POST["links"]))
    {
    $remove=$_POST["links"];
    $remove=mysql_query("DELETE FROM leftmenulink WHERE links = $remove");
    
    if(!$remove)
    {
    echo "Nothing has removed";
    }
    }
    ?>
    That code was mine final attempt of the form and I hope that may be there is some'one here on the forum who knows how to do it.

  2. #2
    Join Date
    Nov 2012
    Posts
    7
    That's a nice try, but it will never work.

    The table leftmenulink must have the primary key (probably column named `id`).
    Now you should pass that id column to html controls and then read 'em from the post to delete rows in the DB by those keys.

    In general, it may look like this (I haven't checked it so it may not work as is, just an idea here):

    Code:
    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <?php
    include("../../database/sqlconnect.php");
    
    $query=mysql_query("SELECT * FROM leftmenulink");
    
    while($query2=mysql_fetch_array($query))
    {
    echo $query2['links'];
    echo '<input type="checkbox" name="links" value="'. $query2['id'] .'"> <br>';
    }
    ?>
    <input type="submit" value="remove" name="remove">
    </form>
    
    <?php
    if(isset($_POST["remove"]) && isset($_POST["links"]))
    {
    $idsStr= implode( ",", $_POST["links"] );
    $remove=mysql_query("DELETE FROM leftmenulink WHERE id IN ( $idsStr )");
    
    if(!$remove)
    {
    echo "Nothing has removed";
    }
    }
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles