Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP - interactive table?

Options
  • 16-01-2011 1:28am
    #1
    Closed Accounts Posts: 27,857 ✭✭✭✭


    Hey dudes,

    I'm just messing around with PHP/MySQL and trying to get used to it. I've got it working basically, but there's something that I'm trying to do.

    I have a simple form that posts the input to action.php, which enters the info into the table and then displays the updated table like:

    Number|Name|Age
    1|Dave|23
    2|Joe|25
    3|Peter|26

    It works fine, etc.

    Now what I want to add to it is a 'delete' button for each entry, as follows:

    Number|Name|Age|
    1|Dave|23|delete
    2|Joe|25|delete
    3|Peter|26|delete

    However I'm not sure how to go about that :confused: I only know how to interact with the database by using POST for forms, but if I wanted the 'delete' buttons to be anchor tags for simplicity's sake, would that be possible?

    Cheers...

    p.s. I may post some more questions here as they arise :D


Comments

  • Banned (with Prison Access) Posts: 1,332 ✭✭✭desaparecidos


    You could generate a GET url under each button (button could be a styled hyperlink) which queries the action.php page.

    something like:

    action.php?delete=1234

    action.php will have an if statement to check if the GET var 'delete' is set, if so, clean the ID input and place it into an SQL query which will perform the delete operation on the database, or set a boolean in a "deleted" column which indicates the row is not to be shown, but lets you save all data in the database.

    Using GET may not be desirable so as you are generating the output table, you could generate a POST form and delete button for each row. Action will be action.php, delete button could have name 'delete' and value of the ID of the current row.


  • Registered Users Posts: 241 ✭✭fcrossen


    action.php?delete=1234
    [...]
    Using GET may not be desirable so as you are generating the output table, you could generate a POST form and delete button for each row. Action will be action.php, delete button could have name 'delete' and value of the ID of the current row.

    Good practise when you use GET/POST:

    1) sanitise the GET/POST variable, DELETE the row
    2) send a browser redirect to send user back to the listing page (or wherever)

    This will prevent your form from being resubmitted if a user reloads your web page.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Additional good practice, somewhat unrelated to the question, is whether you want a delete to be a soft or hard delete.

    Soft delete is to add two more columns to the table - deleted, boolean, default 'false' & deletedDate default NULL.

    Soft delete allows you to restore anything deleted in error, and by inserting the deleted date, you can later on do maintenance and clear out anything deleted more than 6 months ago or whatever.

    As part of your delete operation, I would also always make it 2-step. Have the user click the "delete" link, and then prompt them to confirm it on the next page. The "delete" link uses GET variables, then they get a form which POSTs the confirmation and deletes the entry, before redirecting back to the listing.

    I know that you're only messing with it, but these are handy things to implement for practice.


Advertisement