morocco-iceberg Posted May 29, 2010 Share Posted May 29, 2010 This code has options for adding and deleting data from a table. Adding data works, my problem arises when trying to echo out the current data with a delete button for each row. The data echos out fine, but the delete buttons do not work. Help? if(isset($_POST["add"])){ $a = mysql_real_escape_string($_POST["a"]); $b = mysql_real_escape_string($_POST["b"]); $c = mysql_real_escape_string($_POST["c"]); $d = mysql_real_escape_string($_POST["d"]); $e = mysql_real_escape_string($_POST["e"]); if(!$a || !$b || !$c || !$d || !$e) { die("Please <a href=\"edit_writing.php\">try again</a> and ensure the form is completed properly."); } mysql_query("INSERT INTO writing (a, b, c, d, e) VALUES ('$a', '$b', '$c', '$d', '$e')") or die($error_message); $success_message = "Question was added."; } echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> <title>Title</title> <link rel=\"stylesheet\" type=\"text/css\" href=\"css.css\" /> </head> <body> <div id=\"timer\"> </div> <div id=\"wrap\"> <div id=\"mainheader\"> </div> <div id=\"content\"> <div id=\"orangeheader\"><br/> Grammar Test </div> <div id=\"text\"> " . $success_message . " <table> <tr> <td><strong>Question One</strong></td> <td><strong>Question Two</strong></td> <td><strong>Question Three</strong></td> <td><strong>Question Four</strong></td> <td><strong>Question Five</strong></td> </tr>"; $result = mysql_query("SELECT * FROM writing"); $number = mysql_numrows($result); $counter = 0; while ($counter<$number){ $a = mysql_result($result, $counter,"a"); $b = mysql_result($result, $counter,"b"); $c = mysql_result($result, $counter,"c"); $d = mysql_result($result, $counter,"d"); $e = mysql_result($result, $counter,"e"); echo "<tr> <td>" . $a . "</td> <td>" . $b . "</td> <td>" . $c . "</td> <td>" . $d . "</td> <td>" . $e . "</td> <td> <form action=\"edit_writing.php\" method=\"post\"> <input type=\"submit\" name=\"" . $counter . "\" value=\"Delete\"/> </form> </td> </tr>"; if(isset($_POST["$counter"])){ $a = mysql_real_escape_string($_POST["a"]); mysql_query("DELETE FROM writing WHERE question='$a'") or die($error_message); $success_message = "Question was deleted."; } $counter++; } ?> </table> <form action="edit_writing.php" method="post"> <table> <tr> <td><label for="a">Question One</label></td> <td><label for="b">Question Two</label></td> <td><label for="c">Question Three</label></td> <td><label for="d">Question Four</label></td> <td><label for="e">Question Five</label></td> </tr> <tr> <td><input type="text" name="a" /></td> <td><input type="text" name="b" /></td> <td><input type="text" name="c" /></td> <td><input type="text" name="d" /></td> <td><input type="text" name="e" /></td> </tr> </table> <input type="submit" name="add" value="Add Question"/> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/203280-trying-to-get-multiple-delete-buttons/ Share on other sites More sharing options...
ignace Posted May 29, 2010 Share Posted May 29, 2010 Create one form for the entire table and use <button> instead of input. <button type="submit" name="delete" value="' . $counter . '" class="delete">Delete</button> Quote Link to comment https://forums.phpfreaks.com/topic/203280-trying-to-get-multiple-delete-buttons/#findComment-1065036 Share on other sites More sharing options...
morocco-iceberg Posted May 29, 2010 Author Share Posted May 29, 2010 Oh cool, I've never seen that before. How exactly would I use it? =/ Quote Link to comment https://forums.phpfreaks.com/topic/203280-trying-to-get-multiple-delete-buttons/#findComment-1065054 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.