Jump to content

Archived

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

pikemsu28

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

Recommended Posts

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:
[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);
?>
[/code]

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:
[code]
if(isset($_POST['delete'])){
  foreach($_POST['delete'] as $i => $message_id){
    mysql_query("DELETE * FROM message WHERE messageID='[b]$message_id[/b]'");
  }
}[/code]

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

Share this post


Link to post
Share on other sites
See

http://www.phpfreaks.com/forums/index.php/topic,107464.msg431636.html#msg431636

Share this post


Link to post
Share on other sites
Barand,

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

here is the updated code:
[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);
?>
[/code]

did i place the code in the right spot?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

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

echo "Selected items deleted";
?>
[/code]
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.

Share this post


Link to post
Share on other sites
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.

[code]]
<?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);
}
}
?>
[/code]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Good. I couldn't test it as I don't have your data. But I guess I'd have missed that condition though.

Share this post


Link to post
Share on other sites

×

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.