DeanWhitehouse Posted May 15, 2008 Share Posted May 15, 2008 how can i have people select entry's to delete with checkboxes, how would i check to see what entry to delete? this is what i have so far <?php if(isset($_POST['delete'])) { $newname = mysql_real_escape_string($_POST['newname']); $newcontent = mysql_real_escape_string($_POST['newcontent']); $runame = mysql_real_escape_string($_POST['ru_name']); $rucont = mysql_real_escape_string($_POST['ru_content']); if ($newname && $newcontent) { mysql_query("INSERT INTO `darkflame_news` (id, name, content, time) VALUES( '','$newname','$newcontent', '$date')") or die('Error ' . mysql_error()); echo "<tr><td>News Saved</td></tr>"; if ($runame && $rucont) { mysql_query("INSERT INTO `darkflame_news_ru`(id, name, content, time) VALUES( '','$runame','$rucont', '$date')") or die('Error ' . mysql_error()); } } elseif (!newname.newcontent) { echo "<tr><td>Please Fill In The Required Fields</td></tr>"; } } if(isset($_GET['delete_news'])) { ?> <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> <table class="news_profile"><tr><th>Delete News</th><td class="delete">Delete</td></tr><tr><td align="left"> <?php //No ID passed to page, display user list: if($_SESSION['lang_en'] == true) { $query = "SELECT id, name, content, time FROM darkflame_news ORDER BY id DESC LIMIT 5"; } else { $query = "SELECT id, name, content, time FROM darkflame_news_ru ORDER BY id DESC LIMIT 5"; } $result = mysql_query($query) or die("Error:" . mysql_error()); if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { $content = $row['content']; $cont = wordwrap($content, 118, "<br />", true); echo "<b>".$row['time']."</b>"; ?> - <a href="?news_id=<?php echo $row['id']; ?>"><?php echo $row['name']; ?></a></td><td><input type="checkbox" name="delete<?php echo $row['id']; ?>"></td></tr><tr align="left"><td><?php echo nl2br(stripslashes($cont));; ?></td></tr> <?php } } if($_SESSION['lang_en'] == true) { if (mysql_num_rows($result) < 1) { echo "No News To Display. <br>Keep Checking Back<br>"; } if (mysql_num_rows($result) > 5) { echo "<br><a href='?news_all'>All News</a>"; } } if($_SESSION['lang_ru'] == true) { if (mysql_num_rows($result) < 1) { echo "(in russian)No News To Display. <br>Keep Checking Back<br>"; } if (mysql_num_rows($result) > 5) { echo "<br><a href='?news_all'>More News</a>"; } } ?> </td></tr><tr><td><input type="submit" name="delete" value="Delete Selected"></td></tr></table> <?php exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/105774-solved-delete-entrys-using-checkboxes/ Share on other sites More sharing options...
The Little Guy Posted May 15, 2008 Share Posted May 15, 2008 the entries need to be in an array, so it would look like this: Note the the [] in the name, the is what creates your array <input type="checkbox" value="1" name="checkB[]" /> <input type="checkbox" value="2" name="checkB[]" /> <input type="checkbox" value="3" name="checkB[]" /> then in your php: foreach($_POST['checkB'] as $checked){ mysql_query("DELETE FROM tableName WHERE id='$checked'"); } Quote Link to comment https://forums.phpfreaks.com/topic/105774-solved-delete-entrys-using-checkboxes/#findComment-542003 Share on other sites More sharing options...
DeanWhitehouse Posted May 15, 2008 Author Share Posted May 15, 2008 ok, i am using this code and it isn't deleting anything <?php if(isset($_POST['delete'])) { foreach($_POST['checkB'] as $checked) { mysql_query("DELETE FROM darkflame_news WHERE id='{$checked}'") or die("Error:" . mysql_error()); } } if(isset($_GET['delete_news'])) { ?> <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> <table class="news_profile"><tr><th>Delete News</th><td class="delete">Delete</td></tr><tr><td align="left"> <?php //No ID passed to page, display user list: if($_SESSION['lang_en'] == true) { $query = "SELECT id, name, content, time FROM darkflame_news ORDER BY id DESC LIMIT 5"; } else { $query = "SELECT id, name, content, time FROM darkflame_news_ru ORDER BY id DESC LIMIT 5"; } $result = mysql_query($query) or die("Error:" . mysql_error()); if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { $content = $row['content']; $cont = wordwrap($content, 118, "<br />", true); echo "<b>".$row['time']."</b>"; ?> - <a href="?news_id=<?php echo $row['id']; ?>"><?php echo $row['name']; ?></a></td><td><input type="checkbox" value="1" name="checkB[]" /></td></tr><tr align="left"><td><?php echo nl2br(stripslashes($cont));; ?></td></tr> <?php } } if($_SESSION['lang_en'] == true) { if (mysql_num_rows($result) < 1) { echo "No News To Display. <br>Keep Checking Back<br>"; } if (mysql_num_rows($result) > 5) { echo "<br><a href='?news_all'>All News</a>"; } } if($_SESSION['lang_ru'] == true) { if (mysql_num_rows($result) < 1) { echo "(in russian)No News To Display. <br>Keep Checking Back<br>"; } if (mysql_num_rows($result) > 5) { echo "<br><a href='?news_all'>More News</a>"; } } ?> </td></tr><tr><td><input type="submit" name="delete" value="Delete Selected"></td></tr></table> <?php exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/105774-solved-delete-entrys-using-checkboxes/#findComment-542038 Share on other sites More sharing options...
The Little Guy Posted May 15, 2008 Share Posted May 15, 2008 Inside the foreach loop, echo out $checked, to see if it is the value you want, and make sure that it exists in the database. Quote Link to comment https://forums.phpfreaks.com/topic/105774-solved-delete-entrys-using-checkboxes/#findComment-542045 Share on other sites More sharing options...
benphp Posted May 15, 2008 Share Posted May 15, 2008 value="1" for your checkbox gives all your checkboxes a 1 value, so when you sql runs you get: DELETE FROM darkflame_news WHERE id='1' You need to give the value of your checkboxes the IDs of the records you want to delete. <?php <input type="checkbox" value="<?php echo $row['id']; ?>" name="checkB[]" /> ?> Quote Link to comment https://forums.phpfreaks.com/topic/105774-solved-delete-entrys-using-checkboxes/#findComment-542046 Share on other sites More sharing options...
DeanWhitehouse Posted May 15, 2008 Author Share Posted May 15, 2008 ok, thanks , anyone know how to make a select all so it actually ticks the boxes, would that be ajax? Quote Link to comment https://forums.phpfreaks.com/topic/105774-solved-delete-entrys-using-checkboxes/#findComment-542050 Share on other sites More sharing options...
The Little Guy Posted May 15, 2008 Share Posted May 15, 2008 that would be javascript the HTML: <a href="javascript:select_all('checkB', '1')" id="cAll">Check all</a> <script type="text/javascript"> function select_all(name, value) { for (i = 0; i < forminputs.length; i++) { // regex here to check name attribute var regex = new RegExp(name, "i"); if (regex.test(forminputs[i].getAttribute('name'))) { if (value == '1') { forminputs[i].checked = true; document.getElementById('cAll').innerHTML = 'Uncheck all' document.getElementById('cAll').href = 'javascript:select_all(\'messageA\', \'0\')'; } else { forminputs[i].checked = false; document.getElementById('cAll').innerHTML = 'Check all' document.getElementById('cAll').href = 'javascript:select_all(\'messageA\', \'1\')'; } } } } </script> Quote Link to comment https://forums.phpfreaks.com/topic/105774-solved-delete-entrys-using-checkboxes/#findComment-542058 Share on other sites More sharing options...
DeanWhitehouse Posted May 15, 2008 Author Share Posted May 15, 2008 thanks for the code, but it doesn't do anything ,have i got it right? <script type="text/javascript"> function select_all(name, value) { for (i = 0; i < forminputs.length; i++) { // regex here to check name attribute var regex = new RegExp(name, "i"); if (regex.test(forminputs[i].getAttribute('name'))) { if (value == '1') { forminputs[i].checked = true; document.getElementById('cAll').innerHTML = 'Uncheck all' document.getElementById('cAll').href = 'javascript:select_all(\'messageA\', \'0\')'; } else { forminputs[i].checked = false; document.getElementById('cAll').innerHTML = 'Check all' document.getElementById('cAll').href = 'javascript:select_all(\'messageA\', \'1\')'; } } } } </script> <?php if ($_SESSION['dark_flame'] == true) { ?> <table class="admin_cp" cellspacing="0" cellpadding="3"><tr><td>Test</td></tr></table> <?php } $time_stamp = date("d.m.Y"); $date = $time_stamp; if ($_SESSION['dark_flame'] == true) { if(isset($_POST['delete'])) { foreach($_POST['checkB'] as $checked) { mysql_query("DELETE FROM darkflame_news WHERE id='{$checked}'") or die("Error:" . mysql_error()); } } if(isset($_GET['delete_news'])) { ?> <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> <table class="news_profile"><tr><th>Delete News</th><td class="delete">Delete</td></tr><tr><td align="left"> <?php //No ID passed to page, display user list: if($_SESSION['lang_en'] == true) { $query = "SELECT id, name, content, time FROM darkflame_news ORDER BY id DESC LIMIT 5"; } else { $query = "SELECT id, name, content, time FROM darkflame_news_ru ORDER BY id DESC LIMIT 5"; } $result = mysql_query($query) or die("Error:" . mysql_error()); if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { $content = $row['content']; $cont = wordwrap($content, 118, "<br />", true); echo "<b>".$row['time']."</b>"; ?> - <a href="?news_id=<?php echo $row['id']; ?>"><?php echo $row['name']; ?></a></td><td align="right"><input type="checkbox" value="<?php echo $row['id']; ?>" name="checkB[]" /></td></tr><tr align="left"><td><?php echo nl2br(stripslashes($cont));; ?> <?php } ?><tr><td><input type="submit" name="delete" value="Delete Selected"></td><td><a href="javascript:select_all('checkB', '1')" id="cAll">Check all</a></td></tr> ?> Quote Link to comment https://forums.phpfreaks.com/topic/105774-solved-delete-entrys-using-checkboxes/#findComment-542068 Share on other sites More sharing options...
The Little Guy Posted May 15, 2008 Share Posted May 15, 2008 in the javascript, find all "messageA" and change them to "checkB" leave the slashes there. Quote Link to comment https://forums.phpfreaks.com/topic/105774-solved-delete-entrys-using-checkboxes/#findComment-542090 Share on other sites More sharing options...
DeanWhitehouse Posted May 15, 2008 Author Share Posted May 15, 2008 still not working , if it is to much trouble i will search the net and find a code , Quote Link to comment https://forums.phpfreaks.com/topic/105774-solved-delete-entrys-using-checkboxes/#findComment-542094 Share on other sites More sharing options...
The Little Guy Posted May 15, 2008 Share Posted May 15, 2008 try this whole thing, and replace "messageA" with "checkB" and give your form an id "mailForm" <form id="mailForm" action="..." method="post"> <script type="text/javascript"> var formblock; var forminputs; function prepare() { formblock= document.getElementById('mailForm'); forminputs = formblock.getElementsByTagName('input'); } function select_all(name, value) { for (i = 0; i < forminputs.length; i++) { // regex here to check name attribute var regex = new RegExp(name, "i"); if (regex.test(forminputs[i].getAttribute('name'))) { if (value == '1') { forminputs[i].checked = true; document.getElementById('cAll').innerHTML = 'Uncheck all' document.getElementById('cAll').href = 'javascript:select_all(\'messageA\', \'0\')'; } else { forminputs[i].checked = false; document.getElementById('cAll').innerHTML = 'Check all' document.getElementById('cAll').href = 'javascript:select_all(\'messageA\', \'1\')'; } } } } if (window.addEventListener) { window.addEventListener("load", prepare, false); } else if (window.attachEvent) { window.attachEvent("onload", prepare) } else if (document.getElementById) { window.onload = prepare; } </script> Quote Link to comment https://forums.phpfreaks.com/topic/105774-solved-delete-entrys-using-checkboxes/#findComment-542109 Share on other sites More sharing options...
DeanWhitehouse Posted May 15, 2008 Author Share Posted May 15, 2008 thanks, Quote Link to comment https://forums.phpfreaks.com/topic/105774-solved-delete-entrys-using-checkboxes/#findComment-542112 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.