Jump to content


Photo

Deleting a Record using Checkboxes.......*RESOLVED*


  • Please log in to reply
7 replies to this topic

#1 pikemsu28

pikemsu28
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts

Posted 27 September 2006 - 08:02 PM

I hope someone can help me with my code....Or atleast point me in the right direction. I've read numerous threads about using checkboxes to delete records from a database but cannot seem to get it to work.

Basically, I want to view records that have been posted by users that will enable them to delete the record by selecting a checkbox and hitting submit.  It should allow them to select multiple records to delete at once.

here is ALL of my code:
<?php session_start();
$_SESSION['MM_empid'] = '2';
include ('library/error_handler.inc.php');
virtual('/Connections/Nursing.php');

$maxRows_report = 20;
$pageNum_report = 0;
if (isset($_GET['pageNum_report'])) {
  $pageNum_report = $_GET['pageNum_report'];
}
$startRow_report = $pageNum_report * $maxRows_report;

mysql_select_db($database_Nursing, $Nursing);
$query_report = "SELECT employee_info.first_name, employee_info.last_name, signups.signup_id, signups.account_id, signups.`date`, signups.shift, signups.team, signups.team_assign FROM employee_info, accounts, signups WHERE employee_info.info_id=accounts.info_id AND accounts.account_id=signups.account_id AND accounts.empid='".$_SESSION['MM_empid']."' ORDER BY employee_info.first_name, signups.team, signups.team_assign, signups.`date`, signups.shift";
$query_limit_report = sprintf("%s LIMIT %d, %d", $query_report, $startRow_report, $maxRows_report);
$report = mysql_query($query_limit_report, $Nursing) or die(mysql_error());
$row_report = mysql_fetch_assoc($report);

?>
<form id="form1" name="form1" method="post" action="">
<table width="600" align="center" cellpadding="2" cellspacing="1" border="1" id="CBT Report">
  <tr id="Labels">
    <th width="30%" align="left" valign="bottom" class="style2">Team</th>
    <th width="20%" align="left" valign="bottom" class="style2">Team Assignment</th>
	<th width="25%" align="left" valign="bottom" class="style2">Date</th>
    <th width="15%" align="left" valign="bottom" class="style2">Shift</th>
	<th width="10%" align="center" valign="bottom" class="style2">Remove</th>
  </tr>
<?php
$tmp_team='';
$tmp_team_assign='';
$tmp_date='';
do { ?>
  <tr class="style2">
      <?php
		$team = ucwords($row_report['team']);
		if($team != $tmp_team){
	  ?>
      <td valign="top" width="30%"><a href=TeamDescription.php><?php echo ucwords($row_report['team']); ?></a></td>
	  <?php
		} else echo "<td width='30%'>&nbsp;</td>";
		$tmp_team = $team;
	  
		$team_assign = ucwords($row_report['team_assign']);
	  	if($team_assign != $tmp_team_assign){
	  ?>
      <td valign="top" width="20%"><?php echo ucwords($row_report['team_assign']); ?></td>
	  <?php
		} else echo "<td width='20%'>&nbsp;</td>";
		$tmp_team_assign = $team_assign;
	  ?>
	  <td valign="top" width="25%"><?php echo $row_report['date']; ?></td>
      <td valign="top" width="15%"><?php echo $row_report['shift']; ?></td>
	  <td valign="top" width="10%" align="center">
	  
	    <label>
	      <input type="checkbox" name="id[]" value="<?php echo $row_report['signup_id']; ?>" />
	      </label>
	  </td>
	  <?php
	 } while ($row_report = mysql_fetch_assoc($report)); 
	 ?>
	 <tr align="center">
      <td colspan="6"><input name="submit" type="submit" value="Delete" /></td>
    </tr>
</table></form>
<?php
mysql_free_result($report);
?>

I use dreamweaver so the connection to the database was thru dreamweaver.

Every thread i've read says to use some varient of this code:
if(isset($_POST['delete'])){
  foreach($_POST['delete'] as $i => $message_id){
    mysql_query("DELETE * FROM message WHERE messageID='[b]$message_id[/b]'");
  }
}

I've tried to modify it to support my code but always receive errors or the page just doesn't load.

I dont know what to do. Any help would be greatly appreciated!!
Jason

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 27 September 2006 - 08:13 PM

See

http://www.phpfreaks....html#msg431636
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 pikemsu28

pikemsu28
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts

Posted 27 September 2006 - 08:22 PM

Barand,

I checked out the thread and tried inserting it into my code......the page would not even load.

here is the updated code:
<?php session_start();
$_SESSION['MM_empid'] = '2';
include ('library/error_handler.inc.php');
virtual('/Connections/Nursing.php');

$maxRows_report = 20;
$pageNum_report = 0;
if (isset($_GET['pageNum_report'])) {
  $pageNum_report = $_GET['pageNum_report'];
}
$startRow_report = $pageNum_report * $maxRows_report;

mysql_select_db($database_Nursing, $Nursing);
$query_report = "SELECT employee_info.first_name, employee_info.last_name, signups.signup_id, signups.account_id, signups.`date`, signups.shift, signups.team, signups.team_assign FROM employee_info, accounts, signups WHERE employee_info.info_id=accounts.info_id AND accounts.account_id=signups.account_id AND accounts.empid='".$_SESSION['MM_empid']."' ORDER BY employee_info.first_name, signups.team, signups.team_assign, signups.`date`, signups.shift";
$query_limit_report = sprintf("%s LIMIT %d, %d", $query_report, $startRow_report, $maxRows_report);
$report = mysql_query($query_limit_report, $Nursing) or die(mysql_error());
$row_report = mysql_fetch_assoc($report);

?>
<form id="form1" name="form1" method="post" action="">
<table width="600" align="center" cellpadding="2" cellspacing="1" border="1" id="CBT Report">
  <tr id="Labels">
    <th width="30%" align="left" valign="bottom" class="style2">Team</th>
    <th width="20%" align="left" valign="bottom" class="style2">Team Assignment</th>
	<th width="25%" align="left" valign="bottom" class="style2">Date</th>
    <th width="15%" align="left" valign="bottom" class="style2">Shift</th>
	<th width="10%" align="center" valign="bottom" class="style2">Remove</th>
  </tr>
<?php
$tmp_team='';
$tmp_team_assign='';
$tmp_date='';
do { ?>
  <tr class="style2">
      <?php
		$team = ucwords($row_report['team']);
		if($team != $tmp_team){
	  ?>
      <td valign="top" width="30%"><a href=TeamDescription.php><?php echo ucwords($row_report['team']); ?></a></td>
	  <?php
		} else echo "<td width='30%'>&nbsp;</td>";
		$tmp_team = $team;
	  
		$team_assign = ucwords($row_report['team_assign']);
	  	if($team_assign != $tmp_team_assign){
	  ?>
      <td valign="top" width="20%"><?php echo ucwords($row_report['team_assign']); ?></td>
	  <?php
		} else echo "<td width='20%'>&nbsp;</td>";
		$tmp_team_assign = $team_assign;
	  ?>
	  <td valign="top" width="25%"><?php echo $row_report['date']; ?></td>
      <td valign="top" width="15%"><?php echo $row_report['shift']; ?></td>
	  <td valign="top" width="10%" align="center">
	  
	    <?php
	      echo "<input type='checkbox' name='delitem[]' value='".$row_report['signup_id']."'>";
	    ?>
	  </td>
	  <?php
	 } while ($row_report = mysql_fetch_assoc($report)); 
	 ?>
	 <tr align="center">
      <td colspan="6"><input name="submit" type="submit" value="Delete" /></td>
    </tr>
</table></form>
<?php
$items = join (',' , $_POST['delitem']);
mysql_query ("DELETE FROM signups WHERE signup_id IN ($items)");

echo "Selected items deleted";
?>
<?php
mysql_free_result($report);
?>

did i place the code in the right spot?

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 27 September 2006 - 08:40 PM

I usually place any update/delete code at the top of the script. That way, when the data is queried and listed, the changes can be seen.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 pikemsu28

pikemsu28
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts

Posted 28 September 2006 - 01:00 PM

It's not working at all.  No matter where i place the code, the page doesn't load.  If i take the code out, the page loads just fine i just can't delete anything.

<?php
$items = join (',' , $_POST['delitem']);
mysql_query ("DELETE FROM signups WHERE signup_id IN ($items)");

echo "Selected items deleted";
?>
no matter where i put this in my code the page doesn't load at all.  I get the Apache Server has encountered a problem error requesting me to send the error report to microsoft then the "page cannot be displayed" shows.

#6 pikemsu28

pikemsu28
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts

Posted 28 September 2006 - 01:58 PM

i've been playing around and i've finally got the page to load.  But when i submit the form, i get the same errors as before and the page doesnt load.  if i echo the query it displays properly but the records are not deleted.

]
<?php
if (!isset($_POST['submit'])) 
{
?>
Form........
<?php
} else {
	$del = $_POST['del'];
	$num = count($del);
	for($i = 0; $i < $num; $i++){
	            $ID = $del[$i];
                         $sql = "DELETE FROM signups WHERE signup_id = '$ID'";
                         echo $sql."<BR><BR>";// echos the query which is right.       
	            mysql_query($sql);
	}
}
?>



#7 pikemsu28

pikemsu28
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts

Posted 28 September 2006 - 02:27 PM

Thanks Barand!!

I feel like a moron!!!!  the user i was using to update the database didn't have DELETE privileges for the table.

it works now!!

thanks so much!

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 28 September 2006 - 02:30 PM

Good. I couldn't test it as I don't have your data. But I guess I'd have missed that condition though.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users