Page 1 of 2
html checkboxes...
Posted: Tue Nov 21, 2006 5:04 pm
by boo_lolly
hey all! i've got a CMS i'm working on and i have a couple of HTML tables that are populated via php loops communicating with the SQL database. got it? good.
at the end of each row in the HTML table there is a checkbox. i need to be able to click as many of them as i want, and then press "delete" and have them all be deleted, and then immediately returned to the exact same page.
each HTML table row is exactly like the SQL row. each row has an ID, so that is probably the way i'd identify each row (it's worked for me so far =P).
so can i get any advice, or direction. i'm almost positive i'm gonna have to store the value of the checkboxes in an array of somesort, somehow but i have NO idea where to even start. thanks!
Posted: Tue Nov 21, 2006 5:14 pm
by Burrito
use an array for your checkboxes and give them values of your row IDs.
Posted: Tue Nov 21, 2006 5:35 pm
by boo_lolly
thanks burrito, but can you give me a code example?
Posted: Tue Nov 21, 2006 7:29 pm
by Burrito
Code: Select all
<input type="checkbox" name="somecheck[]" value="<?=$row['id'];?>">
Posted: Wed Nov 22, 2006 10:03 am
by boo_lolly
i'm having some difficulty getting my foreach() loop to run properly. i have the following error.
Warning: Invalid argument supplied for foreach() in /../../../../../admin_search_results.inc on line 76
Code: Select all
// some code
<input type=checkbox name=delete[] value=". $row['uID'] .">
// some more code
echo "<form method=post action=admin1.php?action=view_all>";
echo "<p align=right><input type=submit value=Delete>";
// connecting to database
foreach($_POST['delete'] as $uID => $c)
{
if($c != NULL)
{
$delete_row = "DELETE FROM my_search_table WHERE uID = $uID";
mysql_query($delete_row);
}
}
what's the problem?
Posted: Wed Nov 22, 2006 10:32 am
by JayBird
Looks like you are trying to run the following before submitting the form
Code: Select all
foreach($_POST['delete'] as $uID => $c)
Posted: Wed Nov 22, 2006 10:42 am
by boo_lolly
what do you mean?
my code is within the 'delete' form but AFTER the 'submit' button. how do i make sure it's not processed until it is clicked? the page HAS to be reloaded, it cannot be forwarded to another 'processing' page.
Posted: Wed Nov 22, 2006 10:45 am
by JayBird
Show ALL your code and we'll be able to help more
Posted: Wed Nov 22, 2006 10:49 am
by boo_lolly
Code: Select all
echo "Number of matches: ". $num_result ."<br />";
echo "<form action=addCouple.php><input type=submit value=Add></form>";
echo "<form action=". $_SERVER['PHP_SELF'] ." method=post>";
echo "<TABLE BORDER=1 WIDTH=720><TR><TH>Bride</TH><TH>Groom</TH><TH>Event Date</TH><TH>Shipping Address</TH><TH>Newlywed Info</TH><TH>Delete</TH></TR>";
for($i=0; $i < $num_result; $i++)
{
$row = mysql_fetch_array($result);
echo "<TR><TD align=center>". $row['brideFname'] ." ". $row['brideLname'] ."</TD><TD align=center>". $row['groomFname'] ." ". $row['groomLname'] ."</TD><TD align=center>". $row['event_month'] ."/". $row['event_day'] ."/". $row['event_year'] ."</TD><TD align=center>". $row['ship_add'] .", ". $row['ship_city'] .", ". $row['ship_state'] .", ". $row['ship_zip'] ."</TD><TD align=center><A HREF=editCouple.php?editID=". $row['uID'] .">Edit</A> / <A HREF=updateRegistry.php?regID=". $row['uID'] .">View</A></TD><TD align=center><input type=checkbox name=delete[] value=". $row['uID'] ."></TD></TR>";
$_POST['editID'];
$_POST['regID'];
$_POST['delete'];
}
echo "</TABLE>";
echo "<form method=post action=admin1.php?action=view_all>";
echo "<p align=right><input type=submit value=Delete>";
@ $db = mysql_connect("yah", "blah", "blah");
mysql_select_db("my_DB", $db);
if(!$db)
{
echo "Error: Could not connect to the database. Please try again later.";
exit;
}
foreach($_POST['delete'] as $uID => $c)
{
if($c != NULL)
{
$delete_row = "DELETE FROM my_search_table WHERE uID = $uID";
mysql_query($delete_row);
}
}
echo "</form></form></p>";
} // <-- close an else-if statement... disregard bracket.
mysql_close($db);
?>
i'm retrieving information from a database and looping through to output an HTML table. the last column in the HTML table is a checkbox for each one. the identifier is $uID (unique ID) affiliated with each row. does that help?
Posted: Wed Nov 22, 2006 11:03 am
by boo_lolly
i don't get the error anymore. i made some changes. but it doesn't work. i can check the checkboxes. and press delete. but it doesn't do anything, but it reloads the page just as it should. and no error. i haven't changed anything with the foreach() loop.
Posted: Wed Nov 22, 2006 12:26 pm
by Burrito
you're deleting using the index of the array, not the value:
try echoing out your delete statement to see what it looks like:
Code: Select all
foreach($_POST['delete'] as $uID => $c)
{
if($c != NULL)
{
$delete_row = "DELETE FROM my_search_table WHERE uID = $c";
echo $delete_row;
mysql_query($delete_row)
or die(mysql_error());
}
}
Posted: Wed Nov 22, 2006 2:34 pm
by boo_lolly
this SHOULD work, guys. but it's not. i printed debugging info... everything is right. it's just STILL not deleting it!!! what's wrong with my code!!!
Code: Select all
/*****DEBUG INFO*******
echo "<pre>";
print_r($_POST['delete']);
echo implode($_POST['delete'], ", ");
echo "</pre>";
**********************/
foreach($_POST['delete'] as $k => $c)
{
// echo $k ." / ". $c ." || "; <-- prints the EXACT correct information for the sql query to be properly executed.
$sql = "DELETE FROM my_search_table WHERE uID = ". $c ."";
mysql_query($sql);
}
but it STILL won't work! what's the deal guys?
Posted: Wed Nov 22, 2006 2:39 pm
by volka
a) Your query is still prone to sql injections.
b ) There's no error handling for the mysql operation.
Code: Select all
$sql = "DELETE FROM my_search_table WHERE uID = ". (int)$c;
echo '<div>Debug: ', htmlentities($sql), "</div>\n";
mysql_query($sql) or die(mysql_error());
STILL having trouble.
Posted: Mon Nov 27, 2006 9:54 am
by boo_lolly
here's what my HTML checkbox looks like...
Code: Select all
<TD align=center><input type=checkbox name=delete[] value=". $row['uID'] .">
here's my foreach loop...
Code: Select all
foreach($_POST['delete'] as $k => $c)
{
//echo $k ." / ". $c ." || "; //<-- prints the EXACT correct information for the sql query to be properly executed.
$sql = "DELETE * FROM my_search_table WHERE uID = ". $c ."";
mysql_query($sql);
}
when i uncomment the debugging line and comment out the other two lines, and click a couple of checkboxes, then press delete, this is the output it gives me...
Code: Select all
0 / 3ZFc7FmpYiGpyTybHKXix14teZsQlQ || 1 / FVWxsWALHwtzWjKX23NsMXwbjImaqh || 2 / pSWURh1EsraukzZwYHwiF4DHJnaB3B
so, what i see is that it's passing the correct information to the foreach loop ($c is the uID), but for some reason or another, my SQL query won't process the request, and i can't figure out why. can anybody help?
Posted: Mon Nov 27, 2006 10:15 am
by Burrito
the uid looks to be alphanumeric, you'll need to quote that in your sql statement. If you put some error checking in your mysql_query() as has been suggested, you'd see this.