Jump to content

check box...


corillo181

Recommended Posts

Give this a try

[code]$query = mysql_query("SELECT id, name FROM table");

while($data = mysql_fetch_array($query))
{
echo "<INPUT TYPE=\"checkbox\" NAME=\"id[]\" VALUE=\"" . $data[id] . "\">" . stripslashes($data['name']) . "\n";
}[/code]
Link to comment
Share on other sites

but what i do to delete it i already got it like that just need to know to to delete it..

[code]<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>[/code]

that how i have it.. i just dont know to to delete it..
Link to comment
Share on other sites

i'm going to assume that what you mean is that you have a list of stuff and you have checkboxes next to them and you want to check 1 or more of them and when you click the submit button it deletes all the checked ones from the table, right? (hint: it helps to be more specific in your questions)

[code]
if ($_POST['delbox']) {
   $delbox = $_POST['delbox'];
   $sql = "delete from userpic where username in(";
   foreach ($delbox as $id) { $sql.="'$id',"; }
   $sql = substr_replace($sql,"",-1);         
   $sql.=") ";

   mysql_query($sql) or die(mysql_error());
}
[/code]

first we check if anything was selected. if so, then we set $delbox to the posted stuff.

then we build the query. you would use the 'IN' statement to delete all usernames inside IN ('x','y,'z'....)

so you use the foreach statement to concactinate the sql string to include all of the selected names. then use a substr_replace to chop off the last char in the string because the foreach loop will end up making it with an extra comma at the end. then you run the query.

Link to comment
Share on other sites

i had no luck with that either.. in the form shoudl it be get or post?
[code]
<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 ($_POST['delbox']) {
   $delbox = $_POST['delbox'];
   $sql = "DELETE FROM userpic WHERE username in(";
   foreach ($delbox as $id) { $sql.="'$id',"; }
   $sql = substr_replace($sql,"",-1);          
   $sql.=") ";

   mysql_query($sql) or die(mysql_error());
}
?>
[/code]
Link to comment
Share on other sites

Try this (Note: You may need to edit the delete query):
[code]<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<?php
    if(isset($_POST['Delete'])
    {
        $ids = implode(', ', $_POST['ids']);
        $query = mysql_query('DELETE FROM userpic WHERE id IN(' . $ids . ')');

        echo '<pre>' . print_r($_POST) . '</pre>';
    }

    $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="ids[]" id="'.$code['name'].'" value="'.$code['name'].'" />';
    }
?>
<input type="submit" name="Delete" value="Delete">
</form>[/code]
Link to comment
Share on other sites

all that does is this
[code]
Array ( [delbox] => Array ( [0] => Picture3.jpg ) [Delete] => Delete )
1
[/code]

when i check any pic it prints out the array name because of the print and it says delete , but it doesn't delete it at all..

i guess just sometihng else missing because if it shows the right name of the checked box and says delete...something missing..
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.