gangsterwanster1 Posted August 12, 2009 Share Posted August 12, 2009 I am using mysql_num_rows() to show some database information in columns/rows. How can i do it so that i can delete the row from the site? Like by adding a command button to delete every row that has a check on the check mark? Quote Link to comment https://forums.phpfreaks.com/topic/169945-delete-table-entry-help/ Share on other sites More sharing options...
wildteen88 Posted August 12, 2009 Share Posted August 12, 2009 Work through this tutorial. It should help you with what you need. Quote Link to comment https://forums.phpfreaks.com/topic/169945-delete-table-entry-help/#findComment-896502 Share on other sites More sharing options...
JonnoTheDev Posted August 12, 2009 Share Posted August 12, 2009 First select the primary key and a title field from the table and loop out the rows within a form. You will create an array of checkboxes with the primary key as the value i.e. print "<form method='post'>\n"; print "<input type='hidden' name='action' value='delete' />\n"; // select data $result = mysql_query("SELECT id, title FROM table ORDER BY title ASC"); while($row = mysql_fetch_object($result)) { print "<input type='checkbox' name='values[]' value='".$row->id."' /> ".$row->title."<br />\n"; } print "<input type='submit' name='submit' value='delete selected' />\n"; print "</form>"; We then will add the code that does the deletion. This should be right at the top of the page, prior to anything being printed on the screen: // delete button selected if($_POST['action'] && $_POST['action'] == 'delete') { if(count($_POST['values'])) { // delete records mysql_query("DELETE FROM table WHERE id IN(".implode(",",$_POST['values']).")"); // reload page header("Location:abc.php"); exit(); } } The whole script will look as follows <?php // delete button selected if($_POST['action'] && $_POST['action'] == 'delete') { if(count($_POST['values'])) { // delete records mysql_query("DELETE FROM table WHERE id IN(".implode(",",$_POST['values']).")"); // reload page header("Location:abc.php"); exit(); } } print "<form method='post'>\n"; print "<input type='hidden' name='action' value='delete' />\n"; // select data $result = mysql_query("SELECT id, title FROM table ORDER BY title ASC"); while($row = mysql_fetch_object($result)) { print "<input type='checkbox' name='values[]' value='".$row->id."' /> ".$row->title."<br />\n"; } print "<input type='submit' name='submit' value='delete selected' />\n"; print "</form>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/169945-delete-table-entry-help/#findComment-896508 Share on other sites More sharing options...
gangsterwanster1 Posted August 12, 2009 Author Share Posted August 12, 2009 First select the primary key and a title field from the table and loop out the rows within a form. You will create an array of checkboxes with the primary key as the value i.e. print "<form method='post'>\n"; print "<input type='hidden' name='action' value='delete' />\n"; // select data $result = mysql_query("SELECT id, title FROM table ORDER BY title ASC"); while($row = mysql_fetch_object($result)) { print "<input type='checkbox' name='values[]' value='".$row->id."' /> ".$row->title."<br />\n"; } print "<input type='submit' name='submit' value='delete selected' />\n"; print "</form>"; We then will add the code that does the deletion. This should be right at the top of the page, prior to anything being printed on the screen: // delete button selected if($_POST['action'] && $_POST['action'] == 'delete') { if(count($_POST['values'])) { // delete records mysql_query("DELETE FROM table WHERE id IN(".implode(",",$_POST['values']).")"); // reload page header("Location:abc.php"); exit(); } } The whole script will look as follows <?php // delete button selected if($_POST['action'] && $_POST['action'] == 'delete') { if(count($_POST['values'])) { // delete records mysql_query("DELETE FROM table WHERE id IN(".implode(",",$_POST['values']).")"); // reload page header("Location:abc.php"); exit(); } } print "<form method='post'>\n"; print "<input type='hidden' name='action' value='delete' />\n"; // select data $result = mysql_query("SELECT id, title FROM table ORDER BY title ASC"); while($row = mysql_fetch_object($result)) { print "<input type='checkbox' name='values[]' value='".$row->id."' /> ".$row->title."<br />\n"; } print "<input type='submit' name='submit' value='delete selected' />\n"; print "</form>"; ?> I get this error; Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\test\test.php on line 21 'while($row = mysql_fetch_object($result)) {' Any idea why i get this? All i added was mysqlconnect // mysqlselect database Quote Link to comment https://forums.phpfreaks.com/topic/169945-delete-table-entry-help/#findComment-896727 Share on other sites More sharing options...
JonnoTheDev Posted August 12, 2009 Share Posted August 12, 2009 please provide your connection code Quote Link to comment https://forums.phpfreaks.com/topic/169945-delete-table-entry-help/#findComment-896748 Share on other sites More sharing options...
gangsterwanster1 Posted August 13, 2009 Author Share Posted August 13, 2009 please provide your connection code I got it to work perfectly for the test but now i am having troubles implementing it in my main code. <?php mysql_connect("localhost", "root", "") mysql_select_db("maindb") // delete button selected if($_POST['action'] && $_POST['action'] == 'delete') { if(count($_POST['input'])) { // delete records mysql_query("DELETE FROM table WHERE id IN(".implode(",",$_POST['input']).")"); // reload page header("Location:test.php"); exit(); } } print "<form method='post'>\n"; print "<input type='hidden' name='action' value='delete' />\n"; // select data $result = mysql_query("SELECT cid, title FROM logs ORDER BY title ASC"); while($row = mysql_fetch_object($result)) { print "<input type='checkbox' name='input[]' value='".$row->id."' /> ".$row->title."<br />\n"; } print "<input type='submit' name='submit' value='delete selected' />\n"; print "</form>"; ?> Can you provide an example that uses the above method but does the following? 1.) Makes four rows (username, date, comment, delete check mark) So like this, Bill - 2009 - Loves - [] check mark John -2009 - Hates - [] checkmark (the four rows are four columns from the database) 2.) Once the information has been read from the database and the rows are shown with the four separate areas then at the bottom of the page can there be 1 delete button, which if i select any of the check marks on the page it automatically deletes it? Sorry for asking for so much i have been working on this for a while and i cant figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/169945-delete-table-entry-help/#findComment-896862 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.