Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

PHP Delete Button HELP

  • 26-03-2014 01:02PM
    #1
    Closed Accounts Posts: 147 ✭✭


    Hello,

    I recently started learning PHP on my own and started to use mysql and apache as well. I made a basic table in mysql and using some php code i displayed the table in a html table in a browser. Now I'd like to add a delete button beside each row and when clicked, it would delete that row. I am very new to this and I'm just practicing. Could anyone please help me? This is the code I have so far:


    <?php

    ini_set('display_errors',1);
    error_reporting(E_ALL);

    mysql_connect('localhost', 'root', '');

    mysql_select_db('testdb');

    $result = mysql_query('select * from products');

    $numrows = mysql_numrows($result);

    //****************************************************************
    print "<table border = 3 style = width:400px>";


    for($i = 0; $i < $numrows; $i++)
    {
    $row = mysql_fetch_row($result);

    print "<tr>";


    foreach($row as $cell)
    {

    print "<td>";
    print $cell;
    print "</td>";


    }
    print "</tr>";


    }


    print "</table>";


    mysql_close();

    ?>

    I also have a delete.php page but I really don't know where to start. I've looked for online tutorials and many say different ways.


Comments

  • Registered Users, Registered Users 2 Posts: 2,062 ✭✭✭Colonel Panic


    First off, use code tags to format code you post on here.

    There are two things you need to do. The first is learn about HTML forms and how to extract form data in a PHP script, the second is to learn how to delete records from tables in MySQL.


  • Registered Users, Registered Users 2 Posts: 291 ✭✭Seridisand


    Kudos for taking the initiative to learn a new skill.

    Since your just playing around with a local server, you could do it something like this, adding a form where you can submit(POST) an integer value...this method will only delete the row with the primary key 1

    The form posts to a separate PHP script that handles the delete
    <form action="myform.php" method="POST">
       <input type="submit" name="delete" value="1" />
    </form>
    
    $query = mysql_query("DELETE FROM testdb WHERE id=$_POST['delete']");
    
    
    
    You should check out some resources like: codeacademy, w3schools, stackoverflow, udacity for tutorials and to search for similar Q&A's...


Advertisement