954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

MySQL Checkbox Multiple data Delete?

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: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
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?

NeoNe
Newbie Poster
1 post since May 2007
Reputation Points: 10
Solved Threads: 0
 

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

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

hookedonphp
Newbie Poster
4 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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

w_3rabi
Junior Poster
160 posts since Dec 2006
Reputation Points: 18
Solved Threads: 9
 
w_3rabi
Junior Poster
160 posts since Dec 2006
Reputation Points: 18
Solved Threads: 9
 

HIii

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

if($_REQUEST['delete']))

{
//code here//
}

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

php_coder
Light Poster
34 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

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 "" 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: .. it does work on my PHP script but it wont work for my "Check all Function" javascript.. damn it.. i need help!!!

it2051229
Junior Poster in Training
82 posts since May 2007
Reputation Points: 7
Solved Threads: 1
 

I m facing the same prob buddy if u got solved then plz share it with me

sajjadpk
Newbie Poster
2 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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.

it2051229
Junior Poster in Training
82 posts since May 2007
Reputation Points: 7
Solved Threads: 1
 

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>
Logan_Yupp
Newbie Poster
3 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

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>';
}
Logan_Yupp
Newbie Poster
3 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

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.....
}
sikka_varun
Junior Poster in Training
94 posts since Dec 2008
Reputation Points: 11
Solved Threads: 12
 

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

Logan_Yupp
Newbie Poster
3 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

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['delete'];
$value = $_POST['check'];
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 "";
}
}
mysql_close();
?>

vivek.rangasamy
Newbie Poster
2 posts since Oct 2009
Reputation Points: 9
Solved Threads: 0
 

The above message Check ur Check BOX will definitely Help
Mr. NeoNe or any one with the same bug..

vivek.rangasamy
Newbie Poster
2 posts since Oct 2009
Reputation Points: 9
Solved Threads: 0
 

echo "";

what is the purpose of the echo statement ??

benila
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 
<?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

webvirus
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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

sammry
Light Poster
45 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You