Search code examples
phpmysqlwhile-loopdelete-row

How to delete a row from mysql via php


Brushing up on php and working on a simple program where I've run into an issue. I can't seem to figure out how to delete a mysql row. I will link my script in a pastie document so you can see how I have it set up.

I'm not familiar with AJAX or Javascript.. so I just made the delete button a form. I'd like to keep it like this for now if I can make it work.

PASTIE HERE


Solution

  • On line 30 you have two inputs named del, and the second one does not contain the name to delete

    echo '<form id="del" method="post"><input type="submit" name="del" value="X" /><input type="hidden" name="del" /></form>';
    

    Need to change to something like -

    echo '<form id="del" method="post"><input type="submit" name="del_submit" value="X" /><input type="hidden" name="del" value="'.$del.'" /></form>';
    

    Then change lines 12-13 to

    if (isset($_POST['del_submit'])) {
    mysql_query("DELETE FROM name WHERE name='".$_POST['del']."'");
    

    Please note that mysql_ functions are depreciated, and you are subject to sql injection.