Page 1 of 1

delete with check box

Posted: Tue May 09, 2006 6:05 pm
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>

Posted: Tue May 09, 2006 6:24 pm
by RobertGonzalez

Code: Select all

<?php
$sql = "DELETE FROM userpic WHERE username='$activeuser'";
?>

Posted: Tue May 09, 2006 6:36 pm
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 :)

Posted: Tue May 09, 2006 7:05 pm
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.

Posted: Tue May 09, 2006 8:52 pm
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);
}
?>

Posted: Tue May 09, 2006 8:54 pm
by hawleyjr
That's because your query has a syntax error. Take a look at the query Evarah posted.

Posted: Wed May 10, 2006 3:56 am
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);
}
}
?>

Posted: Wed May 10, 2006 4:05 am
by JayBird
what's the error?

Posted: Wed May 10, 2006 4:29 am
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..

Posted: Wed May 10, 2006 4:45 am
by JayBird
Is the user given the option to delete more than one item?

Posted: Wed May 10, 2006 10:23 am
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.

Posted: Wed May 10, 2006 6:59 pm
by corillo181
i give up on the chekc box.. nothing seens to work..
i just used the get method to delete..