Jump to content

I am trying to delete multiple rows (checkbox)


vividona

Recommended Posts


<?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];

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

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

NOTE: Took a while to write this so im posting anyway :P.

 


<?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-

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

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" :P.

 

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-

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.