Jump to content

[SOLVED] delete function with checkbox


sicKo

Recommended Posts

I'm trying to delete data from database by ticking the checkbox and press the delete button... but it doesn't deems to work.. help pleasee..

<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("test2",$con);

$sql = "select * from member";
$result = mysql_query($sql,$con);
?>

<form action = "deletetest.php" method = "POST">
<?php 
while($row = mysql_fetch_assoc($result))
{
?>
<tr><td><input type = "checkbox" name = "deletebox" value="<?php echo $row['member_ID']; ?>"></td>
<td><?php echo $row['member_ID']; ?></td>
<td><?php echo $row['FirstName']; ?></td>
</tr><br/>


<?php } ?>

<tr><td><input type = "submit" name = "submit" value = "delete"></td></tr>
</form>

<?php 
if($_POST['submit'] == 'submit')
{
foreach($_POST['deletebox'] as $key)
{
$sql2 = "delete from member where member_ID = '$key'";

mysql_query($sql2,$con);
}
}
?>


 

 

Link to comment
https://forums.phpfreaks.com/topic/155780-solved-delete-function-with-checkbox/
Share on other sites

change this

<tr><td><input type = "checkbox" name = "deletebox" value="<?php echo $row['member_ID']; ?>"></td>

to this

<tr><td><input type = "checkbox" name = "deletebox[]" value="<?php echo $row['member_ID']; ?>"></td>

 

and it should work (I think, someone correct me if im wrong ). Hope that helps!

ah i think i see the problem

<tr><td><input type = "submit" name = "submit" value = "delete"></td></tr>
</form>

<?php 
if($_POST['submit'] == 'submit')

the value of the submit button (named submit) is delete, and you are testing to see if its value is submit

 

change the above to

<tr><td><input type = "submit" name = "submit" value = "submit"></td></tr>
</form>

<?php 
if($_POST['submit'] == 'submit')

 

and it should work

 

hope that helped

 

EDIT: Dam too late haha.

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.