Deleting rows with check boxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Shadowing
Forum Newbie
Posts: 4
Joined: Tue Dec 27, 2011 6:56 pm

Deleting rows with check boxes

Post by Shadowing »

Ive hit a huge wall with trying to delete rows with check boxes on my insite messaging system. wondering if someone could please help me out. i just posted the check box html and the code i try to make work.
right now it just deletes a random row that isnt even checked. Im using pagination on the rows on the page. 10 messages on each page. my pagination and links all work 100 percent fine. id like to use array_map intval also

Code: Select all

 

<?php 

 <td><center><input type="checkbox" name="association[]" value="<?php echo $sent_id[0];?>" id="1" /></center></td>



if(isset($_POST['delete'])) {
	
	
                                 $pieces = array("$sent_id"); // is that how i grab them?
                                 
                                 $glue = implode("-", $pieces);	
	
	
	
	foreach ($_POST['association'] as $sent_id){
	
		
	mysql_query("DELETE FROM `sent` WHERE `sent_id`= '".mysql_real_escape_string($glue)."'");
	
	
	
header("Location: sent.php?sent=1");



	}
}

?> 
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Deleting rows with check boxes

Post by s.dot »

make the name like this..

Code: Select all

name="association[<?php echo $idNumber; ?>]"
The id number should be where $idNumber is, I wasn't sure what it was in your code. Then, when this code is POSTed, it will be an array that will contain id numbers that are checked, like so.. (using sample id numbers)

[text]Array
(
12 => on,
98 => on,
133 => on
)[/text]

Which you could then loop through with code like this:

Code: Select all

if (!empty($_POST['association']))
{
    foreach($_POST['association'] AS $idNumber => $val)
    {
        $idNumber = (int) $idNumber;
        //delete $idNumber here?
    }
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Shadowing
Forum Newbie
Posts: 4
Joined: Tue Dec 27, 2011 6:56 pm

Re: Deleting rows with check boxes

Post by Shadowing »

oh thank you so much for replying
so far I got this
I changed my check box for my html to

Code: Select all

<td><center><input type="checkbox" name="association[<?php echo $sent_id[0];?>]" id="1" /></center></td>
do I need a value?
idk what im doing wrong with posting code and why its all greyed out

Code: Select all

<?php
if(isset($_POST['delete'])) {
	
	if (!empty($_POST['association']))

	foreach($_POST['association'] AS $sent_id => $val)
	{
	$sent_id = (int) $sent_id;
	
	mysql_query('DELETE FROM `sent` WHERE `sent_id`= ' . (int) $sent_id);
	
	}	
	
header("Location: sent.php?sent=1");

} 
>?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Deleting rows with check boxes

Post by s.dot »

Your closing php tag is backwards, but I'm guessing you just did that as a typo on here and not in your actual code? :P

Put some debugging notes in your script to see where it gets to.

Code: Select all

<?php

echo 'page accessed<br />';
if(isset($_POST['delete'])) {
    echo 'post delete is set<br />';
   
   if (!empty($_POST['association']))
   {
        echo 'post association is not empty<br />';
       foreach($_POST['association'] AS $sent_id => $val)
       {
            echo 'array element, sent_id: ' . $sent_id . ', val: ' . $val . '<br />';
           $sent_id = (int) $sent_id;
           echo 'deleting id ' . $sent_id . '<br />';
           mysql_query('DELETE FROM `sent` WHERE `sent_id`= ' . (int) $sent_id);
   
       }
}   

die('redirect is happening if this die statement wasnt here'); 
header("Location: sent.php?sent=1");

}
?>
This will help you see where the script is failing.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Shadowing
Forum Newbie
Posts: 4
Joined: Tue Dec 27, 2011 6:56 pm

Re: Deleting rows with check boxes

Post by Shadowing »

yah it was a typo lol
really appreciate you helping me with this. its driving me insane. im having a hard time getting help cause its the holidays I guess.
i cant do anything atm until i get this check box working :) and I thought i was doing amazing when i got pagination to work. loops and arrays are my huge weak point atm.

i noticed that when I add this in the brackets my select all check box stops working. association[<?php echo $sent_id[0];?>]

Code: Select all

<input type="checkbox" name="association[<?php echo $sent_id[0];?>]" value= "<?php echo $sent_id[0];?>" id="1" />

thats a kewl way to debug and see whats going on in the script

Code: Select all

page accessed
post delete is set
post association is not empty
array element, sent_id: 1, val: 1
deleting id 1
redirect is happening if this die statement wasnt here

Code: Select all

  <?php
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>
<? require("menu.php"); ?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="sitestyle.css" />
<script type="text/javascript">

function checkUncheckAll(groupName, checkState){    
	var groupObj = document.forms[0].elements[groupName];    
	var groupLen = groupObj.length;      
	
	for(var groupIdx=0; groupIdx<groupLen; groupIdx++)    
		{        
			groupObj[groupIdx].checked = checkState;    
			}    
			return;
			}


</script>

</head>
<body>
<table width="88%" height="0" border="0" cellspacing="0">
  
<tr>

    <td height="0" align="center"><a href="inbox.php?inbox=1">Inbox</a>
    </td> <td height="0" align="center"><a href="compose.php">Compose</a></td>
     <td height="0" align="center"><a href="sent.php?sent=1">Sent</a></td>
      <td height="0" align="center"><a href="trash.php">Trash</a></td>
  </tr>
  <tr>

</table>
<br/ >
<br/ >
<form name="frm1" id="frm1" action="" method="post"  
15.onsubmit="javascript:return submitIt('frm1')"> 
  <p>
  <center>


    <table width="60%" border="1" cellspacing="1" cellpadding="0">
      <tr align="center">
       
      </tr>
      <tr align="center">
        <td><input type="checkbox" name="checkAll" value="x" id="checkall" onclick="checkUncheckAll('association[]', this.checked)" /></td>
        <td>Recipient</td>
        <td>Subject</td>
        <td>Sent</td>
        <td>read/unread</td>
        </tr>
<?php 



						// find out how many rows are in the table 
				$count3 = "SELECT  COUNT(*) FROM sent WHERE id = '".($_SESSION['user_id'])."'";;
				$count2 = mysql_query($count3) or trigger_error("SQL", E_USER_ERROR);
				$r = mysql_fetch_row($count2);

				$numrows = $r[0];	
	
						// number of rows to show per page
				$rowsperpage = 10;
						// find out total pages
				$totalpages = ceil($numrows / $rowsperpage);	
	
				
		// get the current page or set a default
if (isset($_GET['sent']) && is_numeric($_GET['sent'])) {   
	
						// cast var as int   
				$currentpage = (int) $_GET['sent'];
	} else {   
						// default page num   
				$currentpage = 1;
}// end if				
	

		// if current page is greater than total pages...
if ($currentpage > $totalpages) {   
						// set current page to last page   
				$currentpage = $totalpages;
}// end if

		// if current page is less than first page...
if ($currentpage < 1) {   
						// set current page to first page   
				$currentpage = 1;

} // end if	

						// the offset of the list, based on current page 
				$offset = ($currentpage - 1) * $rowsperpage;	
	
						// get the info from the db 
				$count3 = "SELECT * FROM sent WHERE id = '".($_SESSION['user_id'])."' ORDER BY time DESC LIMIT $offset, $rowsperpage ";
				$count2 = mysql_query($count3) or trigger_error("SQL", E_USER_ERROR);	
						// while there are rows to be fetched...
		while ($list = mysql_fetch_assoc($count2)) {   
						// these are variables to place in the echo
				$sent_id = $list['sent_id'];
				$recipient = $list['sendto'];
				$subject = $list['subject'];
				$read = $list['read'];
				
				
						// grabs the time offset
					$take3 = "SELECT time_offset FROM users WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'"; 
					$take2 = mysql_query($take3) or die(mysql_error());
					$take1 = mysql_fetch_array($take2);

					$recieved = $list['time'];
								// adds the users time offset to the inputed time
					$time = ($recieved + $take1['time_offset']);					
								// sets default time to UTC then formats the inputed time that was offset with the users offset
					date_default_timezone_set('UTC');
					$user_offset = date('h:i:s a  M/d', $time);  
	
?>



	<tr>
      <td><center><input type="checkbox" name="association[<?php echo $sent_id[0];?>]" value= "<?php echo $sent_id[0];?>" id="1" /></center></td>

    </label></td>
       
    <td><center><a href="search_goaulds.php?goauld=<?php echo $recipient; ?>"><?php echo $recipient ?></a></center></td>  
    
       <td><center><a href="sent_view.php?sent_id=<?php echo $sent_id; ?>"><?php echo $subject ?></a></center></td>
    
       <td><center><?php echo $read ?></center></td>  
 
       <td><center><?php echo $user_offset ?></center></td>
   
     
    </tr>
     
        <?php } // while loop 
        ?>
    </table>

  </center>



<div class="deletecontainer">
<div class="delete">
<br/>
<input type="submit" name="delete" id="delete" value="Delete"></p>
</form>
</div>
</div>

<?php

echo 'page accessed<br />';
if(isset($_POST['delete'])) {
    echo 'post delete is set<br />';
   
   if (!empty($_POST['association']))
   {
        echo 'post association is not empty<br />';
       foreach($_POST['association'] AS $sent_id => $val)
       {
            echo 'array element, sent_id: ' . $sent_id . ', val: ' . $val . '<br />';
           $sent_id = (int) $sent_id;
           echo 'deleting id ' . $sent_id . '<br />';
           mysql_query('DELETE FROM `sent` WHERE `sent_id`= ' . (int) $sent_id);
   
       }
}   

die('redirect is happening if this die statement wasnt here'); 
header("Location: sent.php?sent=1");

}
?>	
</body>
</html>  
Last edited by Shadowing on Wed Dec 28, 2011 12:10 am, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Deleting rows with check boxes

Post by s.dot »

Based on that it should have deleted id 1. Did it? Is that the right id?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Shadowing
Forum Newbie
Posts: 4
Joined: Tue Dec 27, 2011 6:56 pm

Re: Deleting rows with check boxes

Post by Shadowing »

yah i just notice that. the id on the data base is 137689
i double checked my quary though

its in column sent_id
and i have it as

$sent_id = $list['sent_id']

all my echoing data works just fine. sendto, subject, read

I just did echo $sent_id;
and the return was all the sent_id on the page
137689
137688
137687
137686
137685
137684
Post Reply