Jump to content


Photo

check box...


  • Please log in to reply
7 replies to this topic

#1 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 09 May 2006 - 04:09 AM

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?

#2 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 09 May 2006 - 05:46 AM

Give this a try

$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";
}


#3 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 09 May 2006 - 11:04 PM

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

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

that how i have it.. i just dont know to to delete it..

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 09 May 2006 - 11:49 PM

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)

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());
}

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.


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 10 May 2006 - 08:44 AM

i had no luck with that either.. in the form shoudl it be get or post?
<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());
}
?>


#6 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 10 May 2006 - 09:17 AM

Try this (Note: You may need to edit the delete query):
<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>

PHP is my friend (=

#7 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 10 May 2006 - 09:43 AM

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

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

#8 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 10 May 2006 - 09:10 PM

Try doing this so it prints an error message if there's one:
$query = mysql_query('DELETE FROM userpic WHERE id IN(' . $ids . ')') or die(mysql_error());
I used this for deleting private messages from an inbox.
PHP is my friend (=




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users