vividona Posted June 24, 2010 Share Posted June 24, 2010 <?PHP $q = mysql_query("SELECT * FROM articles"); $count = mysql_num_rows($q); echo '<form action="" method="post">'; while($runviwartis = mysql_fetch_array($q)) { $Artid = $runviwartis['artid']; ?> <input type="checkbox" name="confirm[]" value="<?PHP echo $Artid; ?>" /><br /> <?PHP } echo '<input type="submit" name="submit" value="DELETE" /></form>'; if(isset($_POST['submit'])){ $confirm = isset($_POST['confirm']) ? $_POST['confirm'] : ""; if($confirm == ""){ echo "<meta http-equiv='Refresh' content='5; URL=index.php?action=viewartis'/>"; throw new Exception("Please confirm this process!"); } echo $count; for($i=0;$i<$count;$i++){ $del_id = $confirm[$i]; $querydelete = mysql_query("DELETE FROM `articls` WHERE artid='".$del_id."'"); } }else{ //echo "any code here"; } ?> It gave me this error Notice: Uninitialized string offset: 2 in C:\wamp\www\forum\classes\artisys.php on line 218 this is line 218 => $del_id = $confirm[$i]; Link to comment https://forums.phpfreaks.com/topic/205779-i-am-trying-to-delete-multiple-rows-checkbox/ Share on other sites More sharing options...
kenrbnsn Posted June 24, 2010 Share Posted June 24, 2010 When you use checkboxes in an array, only those boxes checked are actually returned to your script. You are using $count, which holds the maximum number of rows that could be deleted as your limiter in the for loop. What you should use is the size of the returned array: <?php for($i=0;$i<count($confirm);$i++){ $del_id = $confirm[$i]; $querydelete = mysql_query("DELETE FROM `articls` WHERE artid='".$del_id."'"); } ?> Ken Link to comment https://forums.phpfreaks.com/topic/205779-i-am-trying-to-delete-multiple-rows-checkbox/#findComment-1076835 Share on other sites More sharing options...
vividona Posted June 24, 2010 Author Share Posted June 24, 2010 Hi I solved this issue. I remove this function function NukeMagicQuotes(){ if (get_magic_quotes_gpc()) { $_GET = array_map('stripslashes', $_GET); $_POST = array_map('stripslashes', $_POST); $_COOKIE = array_map('stripslashes', $_COOKIE); } } I don't know the problem in this function but it gave me the following error Warning: stripslashes() expects parameter 1 to be string, array given in C:\wamp\www\forum\classes\Load.php on line 200 Link to comment https://forums.phpfreaks.com/topic/205779-i-am-trying-to-delete-multiple-rows-checkbox/#findComment-1076836 Share on other sites More sharing options...
ChemicalBliss Posted June 24, 2010 Share Posted June 24, 2010 NOTE: Took a while to write this so im posting anyway . <?PHP $q = mysql_query("SELECT * FROM articles"); $count = mysql_num_rows($q); echo '<form action="" method="post">'; while($runviwartis = mysql_fetch_array($q)) { $Artid = $runviwartis['artid']; ?> <input type="checkbox" name="confirm[]" value="<?PHP echo $Artid; ?>" /><br /> <?PHP } echo '<input type="submit" name="submit" value="DELETE" /></form>'; if(isset($_POST['submit'])){ $confirm = isset($_POST['confirm']) ? $_POST['confirm'] : ""; if($confirm == ""){ echo "<meta http-equiv='Refresh' content='5; URL=index.php?action=viewartis'/>"; throw new Exception("Please confirm this process!"); } echo $count; for($i=0;$i<$count;$i++){ $del_id = $confirm[$i]; $querydelete = mysql_query("DELETE FROM `articls` WHERE artid='".$del_id."'"); } }else{ //echo "any code here"; } ?> It gave me this error Notice: Uninitialized string offset: 2 in C:\wamp\www\forum\classes\artisys.php on line 218 this is line 218 => $del_id = $confirm[$i]; That error tells you that your trying to use a String instead of an Array. You need an else statement on your if, because even if your $confirm == "" then it will still try to loop like an array; <?PHP $q = mysql_query("SELECT * FROM articles"); $count = mysql_num_rows($q); echo '<form action="" method="post">'; while($runviwartis = mysql_fetch_array($q)) { $Artid = $runviwartis['artid']; ?> <input type="checkbox" name="confirm[]" value="<?PHP echo $Artid; ?>" /><br /> <?PHP } echo '<input type="submit" name="submit" value="DELETE" /></form>'; if(isset($_POST['submit'])){ $confirm = isset($_POST['confirm']) ? $_POST['confirm'] : ""; if($confirm == ""){ echo "<meta http-equiv='Refresh' content='5; URL=index.php?action=viewartis'/>"; throw new Exception("Please confirm this process!"); }else{ echo $count; for($i=0;$i<$count;$i++){ $del_id = $confirm[$i]; $querydelete = mysql_query("DELETE FROM `articls` WHERE artid='".$del_id."'"); } } }else{ //echo "any code here"; } ?> Also you can wrap the loop into a string to use in a single mysq query, eg: <?PHP $q = mysql_query("SELECT * FROM articles"); $count = mysql_num_rows($q); echo '<form action="" method="post">'; while($runviwartis = mysql_fetch_array($q)) { $Artid = $runviwartis['artid']; ?> <input type="checkbox" name="confirm[]" value="<?PHP echo $Artid; ?>" /><br /> <?PHP } echo '<input type="submit" name="submit" value="DELETE" /></form>'; if(isset($_POST['submit'])){ $confirm = isset($_POST['confirm']) ? $_POST['confirm'] : ""; if($confirm == ""){ echo "<meta http-equiv='Refresh' content='5; URL=index.php?action=viewartis'/>"; throw new Exception("Please confirm this process!"); }else{ echo $count; $query = "DELETE FROM `articls` WHERE "; // Must have trailing space. for($i=0;$i<$count;$i++){ $query .= "artid='".$confirm[$i]."'"; if(($i+1) < $count){ $query .= " OR "; } } $result = mysql_query($query); } }else{ //echo "any code here"; } ?> Hope this helps, -cb- Link to comment https://forums.phpfreaks.com/topic/205779-i-am-trying-to-delete-multiple-rows-checkbox/#findComment-1076839 Share on other sites More sharing options...
vividona Posted June 24, 2010 Author Share Posted June 24, 2010 Hi ChemicalBliss Still gave me this warning Warning: stripslashes() expects parameter 1 to be string, array given in C:\wamp\www\forum\classes\Load.php on line 200 the code works but when I removed this function function NukeMagicQuotes(){ if (get_magic_quotes_gpc()) { $_GET = array_map('stripslashes', $_GET); $_POST = array_map('stripslashes', $_POST); $_COOKIE = array_map('stripslashes', $_COOKIE); } } what is the issue in my function Link to comment https://forums.phpfreaks.com/topic/205779-i-am-trying-to-delete-multiple-rows-checkbox/#findComment-1076846 Share on other sites More sharing options...
kenrbnsn Posted June 24, 2010 Share Posted June 24, 2010 You're getting that error because the $_POST array is now a multidimensional array and stripslashes can't handle arrays. Ken Link to comment https://forums.phpfreaks.com/topic/205779-i-am-trying-to-delete-multiple-rows-checkbox/#findComment-1076851 Share on other sites More sharing options...
vividona Posted June 24, 2010 Author Share Posted June 24, 2010 You're getting that error because the $_POST array is now a multidimensional array and stripslashes can't handle arrays. Ken Yes, u r right. Thank you kenrbnsn Link to comment https://forums.phpfreaks.com/topic/205779-i-am-trying-to-delete-multiple-rows-checkbox/#findComment-1076856 Share on other sites More sharing options...
ChemicalBliss Posted June 24, 2010 Share Posted June 24, 2010 Just to clarify kens response: The checkboxes you set in your form is an array, and the $_POST is always an array, so you have an array inside an array. you can get around this by creating another function to act as a recurring "stripper" . function NukeMagicQuotes(){ if (get_magic_quotes_gpc()) { $_GET = strip_array($_GET); $_POST = strip_array($_POST); $_COOKIE = strip_array($_COOKIE); } } // This will go through an array and strip the slashes from each string it finds, and calls itself if an array is found (to do the former). function strip_array($array){ // Get a numerically indexed array of keys and values $keys = array_keys($array); $values = array_values($array); // Declare a new array $new_array = array(); for($i=0;$i<count($array);$i++){ if(is_array($values[$i])){ $new_array[$keys[$i]] = strip_array($values[$i]); }else{ $new_array[$keys[$i]] = stripslashes($values[$i]); } } return $new_array; } *untested This should work and will remove any errors you get about stripslashes requiring strings. -cb- Link to comment https://forums.phpfreaks.com/topic/205779-i-am-trying-to-delete-multiple-rows-checkbox/#findComment-1076861 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.