html checkboxes...
Moderator: General Moderators
html checkboxes...
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!
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!
Code: Select all
<input type="checkbox" name="somecheck[]" value="<?=$row['id'];?>">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
what's the problem?
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?
Looks like you are trying to run the following before submitting the form
Code: Select all
foreach($_POST['delete'] as $uID => $c)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.
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.
Last edited by boo_lolly on Wed Nov 22, 2006 10:45 am, edited 1 time in total.
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);
?>you're deleting using the index of the array, not the value:
try echoing out your delete statement to see what it looks like:
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());
}
}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!!!
but it STILL won't work! what's the deal guys?
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);
}a) Your query is still prone to sql injections.
b ) There's no error handling for the mysql operation.
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.
here's what my HTML checkbox looks like...
here's my foreach loop...
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...
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?
Code: Select all
<TD align=center><input type=checkbox name=delete[] value=". $row['uID'] .">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);
}Code: Select all
0 / 3ZFc7FmpYiGpyTybHKXix14teZsQlQ || 1 / FVWxsWALHwtzWjKX23NsMXwbjImaqh || 2 / pSWURh1EsraukzZwYHwiF4DHJnaB3B