Jump to content

Archived

This topic is now archived and is closed to further replies.

corillo181

check box...

Recommended Posts

mm

if i'm pulling records from mysql you i want to put a chekc box next to it with a name that represent the recor dhow i do that?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
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..

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
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..

Share this post


Link to post
Share on other sites
Try doing this so it prints an error message if there's one:
[code]$query = mysql_query('DELETE FROM userpic WHERE id IN(' . $ids . ')') or die(mysql_error());[/code]
I used this for deleting private messages from an inbox.

Share this post


Link to post
Share on other sites

×

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.