tjhilder Posted November 24, 2005 Share Posted November 24, 2005 Hi, I want to delete multiple records via a form, but I'm not sure how I should go about this. if someone could show me how I would edit this code: while ($row = mysql_fetch_array ($r)) { echo "\n<tr>\n<td class=\"commentid-number\">{$row['id']}</td>\n<td class=\"commentid-user\">{$row['user']}</td><td class=\"commentid-date\">{$row['date']}</td>\n<td class=\"commentid-edit\"><a href=\"$url?show=edit&id={$row['id']}\">click</a></td>\n<td class=\"commentid-delete\"><a href=\"$url?show=delete&id={$row['id']}\">click</a></td>\n</tr>"; } so that instead of <a href=\"$url?show=delete&id={$row['id']}\">click</a> it would be <input type="checkbox" name="id" value="{$row['id']}"> I think that part is right (if not, please tell me), but I don't know how I would go about it with the query. can someone help me with this? thanks in advanced. -- TJ Link to comment https://forums.phpfreaks.com/topic/2932-deleting-multiple-entries/ Share on other sites More sharing options...
tjhilder Posted November 26, 2005 Author Share Posted November 26, 2005 anyone able to help? Link to comment https://forums.phpfreaks.com/topic/2932-deleting-multiple-entries/#findComment-9876 Share on other sites More sharing options...
ryanlwh Posted November 26, 2005 Share Posted November 26, 2005 name the checkboxes like this <input type="checkbox" name="id[]" value="{$row['id']}"> this would produce echo $_POST['id'][0]; //first id echo $_POST['id'][1]; //second id etc Link to comment https://forums.phpfreaks.com/topic/2932-deleting-multiple-entries/#findComment-9883 Share on other sites More sharing options...
tjhilder Posted November 27, 2005 Author Share Posted November 27, 2005 ah, thanks for reply. does that mean that if I use this mysql query: $query = "DELETE FROM news WHERE news_id={$_POST['id']} that it will delete all the id's or is there more code needed? Edit: I thought I'd try it, but it didn't work (as I expected) got this error: Unknown column 'Array' in 'where clause'. The query was DELETE FROM news WHERE news_id=Array. Link to comment https://forums.phpfreaks.com/topic/2932-deleting-multiple-entries/#findComment-9884 Share on other sites More sharing options...
ryanlwh Posted November 27, 2005 Share Posted November 27, 2005 you need to loop through the array foreach ($_POST['id'] as $v) { $query = "DELETE FROM news WHERE news_id='$v'"; mysql_query($query); } Link to comment https://forums.phpfreaks.com/topic/2932-deleting-multiple-entries/#findComment-9902 Share on other sites More sharing options...
tjhilder Posted November 29, 2005 Author Share Posted November 29, 2005 I have my code like this now, but it turned up an error. if (isset ($_POST['submit'])) { // Handle the form. // Define the query. foreach ($_POST['id'] as $v) { $query = "DELETE FROM news WHERE news_id='$v'"; $r = mysql_query ($query); // Execute the query. } // Report on the result. if (mysql_affected_rows() == 1) { echo "\t\t\t\t\t<div class=\"news-box\">\n\t\t\t\t\t\t<div class=\"news-title\">\n\t\t\t\t\t\t\t:: Success\n\t\t\t\t\t\t</div>\n\t\t\t\t\t\t<div class=\"news-article\">\n\t\t\t\t\t\t\tThe Article has been deleted.<br /><br /><a href=\"$url?show=list\">Back</a>\n\t\t\t\t\t\t</div>\n\t\t\t\t\t</div>\n"; } else { echo "\t\t\t\t\t<div class=\"news-box\">\n\t\t\t\t\t\t<div class=\"news-title\">\n\t\t\t\t\t\t\t:: Error\n\t\t\t\t\t\t</div>\n\t\t\t\t\t\t<div class=\"news-article\">\n\t\t\t\t\t\t\tMySQL Error Information: <b>" . mysql_error() . "</b>. The query was $query.\n\t\t\t\t\t\t</div>\n\t\t\t\t\t</div>\n"; } } MySQL Error Information: . The query was DELETE FROM news WHERE news_id=''. I have the code like this in another part of my code: <input type="checkbox" name="id[]" value="{$row['id']}"> Link to comment https://forums.phpfreaks.com/topic/2932-deleting-multiple-entries/#findComment-9930 Share on other sites More sharing options...
ryanlwh Posted November 29, 2005 Share Posted November 29, 2005 you should put that if/else clause inside the loop. Link to comment https://forums.phpfreaks.com/topic/2932-deleting-multiple-entries/#findComment-9941 Share on other sites More sharing options...
tjhilder Posted November 30, 2005 Author Share Posted November 30, 2005 Ok I did as you said: // Define the query. foreach ($_POST['id'] as $v) { $query = "DELETE FROM news WHERE news_id='$v'"; $r = mysql_query ($query); // Execute the query. // Report on the result. if (mysql_affected_rows() >= 1) { echo "\t\t\t\t\t<div class=\"news-box\">\n\t\t\t\t\t\t<div class=\"news-title\">\n\t\t\t\t\t\t\t:: Success\n\t\t\t\t\t\t</div>\n\t\t\t\t\t\t<div class=\"news-article\">\n\t\t\t\t\t\t\tThe Article has been deleted.<br /><br /><a href=\"$url?show=list\">Back</a>\n\t\t\t\t\t\t</div>\n\t\t\t\t\t</div>\n"; } else { echo "\t\t\t\t\t<div class=\"news-box\">\n\t\t\t\t\t\t<div class=\"news-title\">\n\t\t\t\t\t\t\t:: Error\n\t\t\t\t\t\t</div>\n\t\t\t\t\t\t<div class=\"news-article\">\n\t\t\t\t\t\t\tMySQL Error Information: <b>" . mysql_error() . "</b>. The query was $query.\n\t\t\t\t\t\t</div>\n\t\t\t\t\t</div>\n"; } } now I get 2 error messages (probably cos I was trying to delete 2 things at once): MySQL Error Information: . The query was DELETE FROM news WHERE news_id=''. Link to comment https://forums.phpfreaks.com/topic/2932-deleting-multiple-entries/#findComment-9945 Share on other sites More sharing options...
ryanlwh Posted November 30, 2005 Share Posted November 30, 2005 it's weird that mysql_error() was empty. View source on the form page, see if the checkboxes have values, and make sure you don't have something named id elsewhere. Link to comment https://forums.phpfreaks.com/topic/2932-deleting-multiple-entries/#findComment-9967 Share on other sites More sharing options...
tjhilder Posted December 1, 2005 Author Share Posted December 1, 2005 Ah! it's working now! thanks for your help. I found that it was trying to get 'id' from the mysql table but it's not id, it's news_id so I changed that, I also changed the foreach () with id to article_id (just in case it mixed with something else) wasn't and id[] to article_id[] for same reason. although one thing I do want to fix now is the multiple messages that I get, because the mysql_affected_rows() is in the foreach, is there a way around this? EDIT: I do believe that I might have solved it, although if you have a better idea then mine i'd like it -------------- changed echo "error"; or echo "success"; for $information = "error"; or $information = "success"; -------------- then did an echo $information; outside the foreach statement (so if nothing there it just remains blank.) Link to comment https://forums.phpfreaks.com/topic/2932-deleting-multiple-entries/#findComment-9977 Share on other sites More sharing options...
ryanlwh Posted December 2, 2005 Share Posted December 2, 2005 or you could put those success/error messages into an array (or concatenate into a string), and print the array or string after the loop. There really is no "best" way. Link to comment https://forums.phpfreaks.com/topic/2932-deleting-multiple-entries/#findComment-9980 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.