Jump to content

Delete Script using Checkboxes


cadac

Recommended Posts

Hi,

 

I need some help figuring out my problem. I currently have a script that retrieves records from a mysql database, then has checkboxes to enable to delete a record?

 

Any help where I might be going wrong as it displays, but doesn't delete??

 

Thanks

 

 

<?php

require_once('includes/config.php');

$sql="SELECT * FROM members ORDER BY member_id";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="413" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td colspan="5" bgcolor="#FFFFFF"><strong>Delete a Member</strong> </td>
</tr>
<tr>
<td width="48" align="center" bgcolor="#FFFFFF">#</td>
<td width="42" align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td width="129" align="left" bgcolor="#FFFFFF"><strong>First Name</strong></td>
<td width="152" align="left" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
<td width="152" align="left" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_assoc($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['member_id']; ?>"></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['member_id']; ?></td>
<td align="left" bgcolor="#FFFFFF"><? echo $rows['firstname']; ?></td>
<td align="left" bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="left" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this 
if($delete){
for($i=0;$i<$count;$i++){
$id = $checkbox[$i];
$sql = "DELETE FROM members WHERE member_id='$id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php 
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete-member.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>

Link to comment
Share on other sites

It would be much easier to develop locally, using a stack such as LAMP, MAMP or WAMP. Then you wouldn't have to upload files every time you make the tiniest change.

 

For the moment however, add the following to the top of the script, right after the opening <?php tag

 

error_reporting(-1);
ini_set('display_errors', 1);

Link to comment
Share on other sites

Thanks, worked this time ;)

 

Notice: Undefined variable: delete in /home/upckayju/public_html/members/admin/delete-member.php on line 102

 

I've looked at this and its the submit button which i changed to this: if(isset($_POST['delete'])){ but this still didn't delete a row?

Link to comment
Share on other sites

Solved, took some concentration finding out what was wrong. The checkbox value wasn't declared so it was processing it correctly

 

<?php

require_once('includes/config.php');

$sql="SELECT * FROM members ORDER BY member_id";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="413" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td colspan="5" bgcolor="#FFFFFF"><strong>Delete a Member</strong> </td>
</tr>
<tr>
<td width="48" align="center" bgcolor="#FFFFFF">#</td>
<td width="42" align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td width="129" align="left" bgcolor="#FFFFFF"><strong>First Name</strong></td>
<td width="152" align="left" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
</tr>
<?php
while($rows=mysql_fetch_assoc($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['member_id']; ?>"></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['member_id']; ?></td>
<td align="left" bgcolor="#FFFFFF"><? echo $rows['firstname']; ?></td>
<td align="left" bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="left" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this 
if(isset($_POST['delete'])){
$checkbox=$_POST['checkbox'];
for($i=0;$i<count($checkbox);$i++){
$id = $checkbox[$i];
$sql = "DELETE FROM members WHERE member_id='$id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php 
if($result){

}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.