SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2001
    Location
    home
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Deleting Records with PHP/MySQL

    I am in the process of designing a web based gui page that gives somone the option so editing or deleting a record in a database. Ideally, I would like to have the Edit/Delete links side by side. The edit links bring the user to a special form that is populated with the data from that record and they can edit the data from there. However, I would like to have the delete link simply delete that record when clicked on and return the user back to the edit/delete form. Is this possible and what would be the syntax to do so?

    I've searched the PHP/MySQL books, but haven't found exactly what I'm looking for.

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    you need to somehow pass an identification for the record either via a GET variable or a hidden form field. A GET variable is probably the easiest way, but it can be misused as easily.

    This is an example using a GET variable.

    PHP Code:
    // display.php -- displaying the records
    while ($data mysql_fetch_row($query)) {
        
    // in this example $data[0] holds the record id
        // display the record here
        // add an delete link using:
        
    echo '<a href="delete.php?id=' $data[0] . '">delete record</a>';

    And on page delete.php you can access the parameter which was passed via the QUERY_STRING. You probably need to add some safety checks to make sure that only the selected records are deleted.

    PHP Code:
    // delete.php -- deleting records
    $id = (int)$_GET['id'];
    mysql_query("DELETE FROM yourtable WHERE id = $id");
    // redirect the user to another url
    header('Location: http://www.domain.com/page.php');
    exit(); 
    Christian

  3. #3
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is fairly simple and there are a multitude of ways to do it. The simplest is probably:

    if your page was called page.php then next to your item to be deleted you would have a delete link which went to page.php?action=delete&id=whatever [this is some link - how you reference your rows in the db)

    Once clicked it reloads the page. You need to check if action was set to delete:

    PHP Code:
    if(isset($_GET["action"]) && $_GET["action"]=="delete") { 
    Then just run your delete syntax. It'd be worth checking that the id you use is valid (i.e stop malicious users having a go at your DELETE command... for example, make sure the id is an int with is_int for example up to you really).

    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2001
    Location
    home
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This looks like what I'm looking for. I'll give this a try. Thanks Christian!

  5. #5
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it is possible.

    You could do it several ways. The first way would be to populate the link like this:
    Code:
    <a href="[add/delete form script]?action=delete&record=[record number]
    </DIV><DIV> </DIV><DIV>Another way to do it would be to have a "submit" button that has a value of "Delete":
    Code:
    <form action="[add/delete form script]" method="post">
    <input type="hidden" name="record" value="[record number]" />
    <input type="submit" name="submit" value="Delete" />
    </form>
    Then at the top of your add/delete form script you would test for a value of $_POST['submit']. If this has the value of "Delete" you would retrieve the record number this way:
    PHP Code:
    $record $_POST['record'];
    // Then you would execute your query here
    mysql_query"DELETE FROM [table name] WHERE userId = '$userId' AND recordId = $record"); 
    This is, of course, quite sketchy, but hopefully it will give you a bump in the right direction.
    John

  6. #6
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may also want to add a confimation javascript popup and/or a confirmation page.

    (depending on what sort of data it is of course)
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.


Bookmarks

Posting Permissions

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