delete with check box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

delete with check box

Post by corillo181 »

i already got this done i just dont know what php code would delete it i tried a few but it does not work..

Code: Select all

<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>"><?php 
$userpic="SELECT name FROM userpic WHERE username='$activeuser' LIMIT 4";
$userquery=mysql_query($userpic);
while($code=mysql_fetch_array($userquery)){
echo'<img border=2 bordercolor=#000000 height=100 width=100 src="userpic/'.$code['name'].'">'.'<input type="checkbox" name="delbox[]" id="'.$code['name'].'" value="'.$code['name'].'" />';
}
?>
<p><input type="submit" name="Delete" value="Delete">
</form>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
$sql = "DELETE FROM userpic WHERE username='$activeuser'";
?>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Everah wrote:

Code: Select all

<?php
$sql = "DELETE FROM userpic WHERE username='$activeuser'";
?>
Before you can just delete stuff you will need to distinguish which 'active user' you are trying to delete. For starters you should name each check box with some type of identification. For example, use the 'id' field in your query:

Code: Select all

$userpic="SELECT id,name FROM userpic WHERE username='$activeuser' LIMIT 4";
$userquery=mysql_query($userpic);
while($code=mysql_fetch_array($userquery)){
echo'<img border=2 bordercolor=#000000 height=100 width=100 src="userpic/'.$code['name'].'">'.'<input type="checkbox" name="delbox_'.$code['id'].'" id="'.$code['name'].'" value="'.$code['name'].'" />';
}
And then, on the next page you need to check for each checked box.

Code: Select all

#GET ALL POSSIBLE NAMES
$userpic="SELECT id,name FROM userpic WHERE username='$activeuser' LIMIT 4";
$userquery=mysql_query($userpic);
while($code=mysql_fetch_array($userquery)){
#WAS CHECK BOX CHECKED?
if( isset( $_POST['delbox_'.$code['id'] ) ){
#THIS BOX WAS CHECKED DELETE IT
$sql = "DELETE FROM userpic WHERE id='$code['id']'";
}

}
Now, please note; this is very buggy and you really need validation. However, this will remove desired check boxes...

Note: All code is untested :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Sorry, threw out a quick syntax hint for the OP to follow. SHould have qualified it a little by stating that it was more format than function.
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

this dont' work..

Code: Select all

<?php
if(isset($_POST['delbox'.$code['name']])){
$del=$_POST['delbox'.$code['name']];
$delete="DELETE * FROM userpic WHERE name='$del'";
$check7=mysql_query($delete);
$chekarray=mysql_fetch_array($check7);
}
?>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

That's because your query has a syntax error. Take a look at the query Evarah posted.
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

i did tried it that way but it dos not work

Code: Select all

<form id="form1" name="form1" method="POST" action="<?php $_SERVER['PHP_SELF']; ?>"><?php 
$userpic="SELECT name FROM userpic WHERE username='$activeuser' LIMIT 4";
$userquery=mysql_query($userpic);
while($code=mysql_fetch_array($userquery)){
echo'<img border=2 bordercolor=#000000 height=100 width=100 src="userpic/'.$code['name'].'">'.'<input type="checkbox" name="delbox'.$code['name'].'" id="'.$code['name'].'" value="'.$code['name'].'" />';
}
?>
<p><input type="submit" name="Delete" value="Delete">
</form>
<?php
if(isset($_POST['Delete']))
{
if ($_POST['delbox'.$code['name']]) {
   $delbox = $_POST['delbox'.$code['name']];
   $sql = "DELETE FROM userpic WHERE username='$activeuser'";
   $query=mysql_query($sql) or die(mysql_error());
   $fh=mysql_fetch_array($query);
}
}
?>
and

Code: Select all

<form id="form1" name="form1" method="POST" action="<?php $_SERVER['PHP_SELF']; ?>"><?php 
$userpic="SELECT name FROM userpic WHERE username='$activeuser' LIMIT 4";
$userquery=mysql_query($userpic);
while($code=mysql_fetch_array($userquery)){
echo'<img border=2 bordercolor=#000000 height=100 width=100 src="userpic/'.$code['name'].'">'.'<input type="checkbox" name="delbox'.$code['name'].'" id="'.$code['name'].'" value="'.$code['name'].'" />';
}
?>
<p><input type="submit" name="Delete" value="Delete">
</form>
<?php
if(isset($_POST['Delete']))
{
if ($_POST['delbox'.$code['name']]) {
   $delbox = $_POST['delbox'.$code['name']];
   $sql = "DELETE * FROM userpic WHERE username='$activeuser' AND name='$delbox'";
   $query=mysql_query($sql) or die(mysql_error());
   $fh=mysql_fetch_array($query);
}
}
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

what's the error?
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

Pimptastic wrote:what's the error?
there is no error.. it just doesn't do anything..

is like if i have the hceck boxes and when i click delete the page submit to it self, but nothing happens..
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Is the user given the option to delete more than one item?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Your code for your form should be...

Code: Select all

<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<?php
$sql = "SELECT name 
		FROM userpic 
		WHERE username='$activeuser' 
		LIMIT 4";
if (!$result = mysql_query($sql)) {
	die("There was problem with this query: " . mysql_error());
}

while ($code = mysql_fetch_array($userquery)) {
	echo '<img border=2 bordercolor=#000000 height=100 width=100 src="userpic/'.$code['name'].'">'.'<input type="checkbox" name="delbox[' . $code['name'] . ']" id="'.$code['name'].'" value="'.$code['name'].'" />';
}
?>
<p><input type="submit" name="Delete" value="Delete">
</form>
One possibility for your delete code could be something like...

Code: Select all

<?php
if (isset($_POST['delbox'])) {
	if (is_array($_POST['delbox'])) {
		foreach($_POST['delbox'] as $username => $checkdelete) {
			if ($checkdelete == 'On') {
				$sql = "DELETE FROM userpic WHERE username = '$username'";
				$result = mysql_query($sql) or die("Cannot delete $username from the database: " . mysql_error());
				if (mysql_affected_rows($result) == 0) {
					echo "User $username was not deleted...";
				} else {
					echo "User $username was deleted...";
				}
			}
		}
	}
}
?>
I am sure there is another, more efficient way to do this. The thing to remember when using checkboxes is that the value of the item you are passing needs to be the index of the checkbox in the HTML form. Then, when the form is submitted, the checkboxes become and array, and you need to check which have a value of 'On' (which is checked), then do something with those.

If there is another formfield element you could use, I would maybe try those if you want to make identifying your passed data easier to identify.

Hope this helps.
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

i give up on the chekc box.. nothing seens to work..
i just used the get method to delete..
Post Reply