corillo181 Posted May 9, 2006 Share Posted May 9, 2006 mmif 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? Quote Link to comment Share on other sites More sharing options...
DylanBlitz Posted May 9, 2006 Share Posted May 9, 2006 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] Quote Link to comment Share on other sites More sharing options...
corillo181 Posted May 9, 2006 Author Share Posted May 9, 2006 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.. Quote Link to comment Share on other sites More sharing options...
.josh Posted May 9, 2006 Share Posted May 9, 2006 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. Quote Link to comment Share on other sites More sharing options...
corillo181 Posted May 10, 2006 Author Share Posted May 10, 2006 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><?phpif ($_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] Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted May 10, 2006 Share Posted May 10, 2006 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] Quote Link to comment Share on other sites More sharing options...
corillo181 Posted May 10, 2006 Author Share Posted May 10, 2006 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.. Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted May 10, 2006 Share Posted May 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.