Hi to everyone I’m wondering how to make some simple checkbox for multiple data delete from mysql I found some tutorial on net but don’t work. I tested it without changing anything and doesn’t work. I’m wondering did programmer test it before he posted that tutorial ?

Here is a link of that tutorial if u don’t believe me test it.
phpeasystep.com

I’m working on some cms tool to make posts on site I used a lot of cms like Joomla, WordPress and Textpattern. Now I want to make something like that, of course not even similar to that but just for my personal use and fun.

2 things to do:

  1. Option to select all checkboxes. I like something like in joomla in top there is checkbox with titles when u select it all checkbox are selected and if u uncheck it all is unchecked
  2. When I press delete button all checkboxes that I select are deleted from database or moved to some other category like trash which is nice because u can in that way return some posts that u delete by mistake or some other reason

Here is my code that bothers me becouse checkboxes don't work :(

<?php
include ('conf/config.php');
include('conf/opendb.php');
$sql="SELECT id, title, author, DATE_FORMAT(date, '%M %d, %Y') as sd FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table border="0" cellpadding="0" cellspacing="0" class="adminlist">
<tr id="top">
<th width="5" align="center">Id</th>
<th width="5" align="center">#</th>
<th>Title</th>
<th width="200" align="center">Author</th>
<th width="100" align="center">Date</th>
<th width="80" align="center">Delete</th>
<th width="20" align="center">Edit</th>
</tr>
<?php
while($rows = mysql_fetch_array($result)){
?>
<tr>
<td width="5" align="center"><? echo $rows['id']; ?></td>
<td width="5" align="center"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td><? echo $rows['title']; ?></td>
<td width="200" align="center"><? echo $rows['author']; ?></td>
<td width="100" align="center"><? echo $rows['sd']; ?></td>
<td width="80" align="center" id="trash"><a href="delete_news.php?id=<? echo $rows['id']; ?>">Delete</a></td>
<td width="20" align="center" id="edit"><a href="edit_news.php?id=<? echo $rows['id']; ?>">Edit</a></td>
<?php
}
?>
<tr>
<td colspan="7" align="center"><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++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

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

what could be wrong?

The author may have register_globals ON, while you have (appropriately) turned it OFF.

Evaluate $_POST to see if the value really is "Delete". Instead of simply
if($delete), then if($_POST == 'Delete')

about the CMS there is another guy on daniweb who is making a CMS for fun
u could work with him if u like so u could come out with something useful

HIii

while checking if delete button is active or not
you should say

if($_REQUEST))

{
//code here//
}

Hope now it will work. let me knw after u try...
Cheers
Prati

Well i'm having a problem with the compatibility of javascript and PHP multiple delete check box.. i used a javascript for the "CHECK ALL BOXES" just like yahoo mail.. so my input is something like this "<input type='checkbox' name='checkbox' value='1'><input type='checkbox' name='checkbox' value='2'>" and the check all boxes function worked but it doesnt work with my PHP code where it is something like this :

$checked = $_POST["checkbox"];
for($i=0; $i < count($checked); $i++) {
....// DELETE ALL CHECKED
}

but if i change my input to something like: <input type='checkbox' name='checkbox[]' value='1'><input type='checkbox' name='checkbox[]' value='2'>.. it does work on my PHP script but it wont work for my "Check all Function" javascript.. damn it.. i need help!!!

Yes this problem of mine was since i was still learning javascript. What i did is the name is still the same where the name has the '[]' brackets so that it will work with PHP but the javascript will be different. If you mind posting your javascript so that i can see.

For The Check All/Uncheck All Functions.
Just Change The Name Of The Form In The Javascript And The HTML As Needed

The Javascript:

function CheckAll() {
	count = document.deleteform.elements.length;
	for (i=0; i < count; i++) {
		if (document.deleteform.elements[i].checked == 0) {
			document.deleteform.elements[i].checked = 1;
		} else {
			document.deleteform.elements[i].checked = 1;
		}
	}
}

function UncheckAll() {
	count = document.deleteform.elements.length;
	for (i=0; i < count; i++) {
		if (document.deleteform.elements[i].checked == 1) {
			document.deleteform.elements[i].checked = 0;
		} else {
			document.deleteform.elements[i].checked = 0;
		}
	}
}

The HTML:

<form name="deleteform" action="somepage" method="post">

	<input type="checkbox" name="ANY NAME" value="THE VALUE" />

	<a href="javascript:void()" onClick = "CheckAll()">Select All</a>
	<a href="javascript:void()" onClick = "UncheckAll()">Select None</a>

</form>

I've been experimenting with my php code for a while now and i found a way to perform the task of deleting the check-boxed items:

In the html, make sure all the check-boxed items have are named with brackets '[]' after the name, Like:

<input type="checkbox" name="delete[]" value="THE VALUE" />

Post the results to the second page. In the second page, assign the posted results to a variable. In my example, the variable is $delete:

$delete = $_POST['delete'];


//Then do what you want with the selected items://

foreach ($delete as $id) {

	$q = "DELETE FROM table_name WHERE item_id = $id LIMIT 1"; //THE QUERY
	$r = @mysqli_query($DATABASE_CONNECTION, $q); //EXECUTE QUERY
}


//Show that the items have been successfully removed.//

if (mysqli_affected_rows($DATABASE_CONNECTION) > 0) {
	echo '<p>The selected items have been successfully deleted.</p>';
} else {
	echo '<p>An error has occurred while processing your request</p>';
}

On the top of the page, just include the following piece of code...

<?php
if($_POST['delete']=="Delete")
{
    $delete_ids=implode(",",$_POST['checkbox']);
    mysql_query("Delete from $tbl_name WHERE id in $delete_ids");
   //Checkbox is the input field named checkbox.. It automatically takes goes as an array in php.

    //Thats it.. you are done.....
}

Thats awesome..i'm a newbie to programming with php so the implode function is new to me..thanks for showing that =)

Hey Buddy!! Try This It will work.. Thing is you are POSTING the check box value but not receiving it.. Hope it may help you.. Cheers..

<?php
// Check if delete button active, start this
$action = $_POST;
$value = $_POST;
if($action == 'Delete')
{
for($i=0;$i<$count;$i++){
$del_id = $value[$i];
$sql = "DELETE FROM $tbl_name WHERE Issue_No='$del_id'";
$result = mysql_query($sql) or die ('Could not connect: ' . mysql_error());
}

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

Comments
Come on. That post is over 2 years old. Why even post.

echo "<meta http-equiv=\"refresh\" content=\"0;URL=post_manage.php\">";

what is the purpose of the echo statement ??

<?php
           if($_POST['delete']=='Delete')
            {
	    for($i=0;$i<$count;$i++)
	    {
	          $del_id=$_POST[checkbox'$i''];
                   }
             }
?>

Please help me
i can't catch the value of $_POST[checkbox[$i]];
when i type

For The Check All/Uncheck All Functions.
Just Change The Name Of The Form In The Javascript And The HTML As Needed

The Javascript:

function CheckAll() {
	count = document.deleteform.elements.length;
	for (i=0; i < count; i++) {
		if (document.deleteform.elements[i].checked == 0) {
			document.deleteform.elements[i].checked = 1;
		} else {
			document.deleteform.elements[i].checked = 1;
		}
	}
}

function UncheckAll() {
	count = document.deleteform.elements.length;
	for (i=0; i < count; i++) {
		if (document.deleteform.elements[i].checked == 1) {
			document.deleteform.elements[i].checked = 0;
		} else {
			document.deleteform.elements[i].checked = 0;
		}
	}
}

The HTML:

<form name="deleteform" action="somepage" method="post">

	<input type="checkbox" name="ANY NAME" value="THE VALUE" />

	<a href="javascript:void()" onClick = "CheckAll()">Select All</a>
	<a href="javascript:void()" onClick = "UncheckAll()">Select None</a>

</form>

Hi. my form name is frmMain, i just replaced with deleteform, and what should i replace with elements as should i keep at as it is, but this did not work for me. can you help please

This article has been dead for over six months. Start a new discussion instead.