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 Delete Button HELP

Options
  • 26-03-2014 2: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 Posts: 2,015 ✭✭✭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 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