Jump to content

html checkboxes....


boo_lolly

Recommended Posts

for some reason i can't get my foreach() loop to process the deletion. i've got one loop that processes the sql query and outputs an HTML table. at the end of each table row, there is a checkbox. what i want to do is, if the checkbox is checked, and the user presses the 'delete' button, all the checked rows will be deleted from the database table. and then, the page reloads, showing the new results (without the recently deleted rows)... so here's what i got. i don't have ANY errors... but it doesn't work. i check the boxes. press delete. nothing happens. just reloads the page...


[code]
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) //LINE 76
{       
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);
?>
[/code]

it's really weird... NOW that i try it... it deletes like every row except one row... and all i did was check a single row... press delete, and it deletes almost all of it. THEN, it gives me an error at the bottom....

[b]Warning: Invalid argument supplied for foreach() in /../../../../../admin_search_results.inc on line 76[/b]

so weird!
Link to comment
Share on other sites

I am not very good at following other peoples code, but from having a quick look, is the delete button within the form that the checkboxes are in?  I might be wrong cos only an amateur myself, but surely when you hit the submit button with the value delete, it is not sending the information from the other form with the checkboxes in?
Link to comment
Share on other sites

A few things:

1) You should verify that [b]$_POST['delete'][/b] contains the values that you expect. Instead of doing the delete function, print the values of that to the page like this:
[code]echo "<pre>";
print_r($_POST['delete']);
echo "<pre>";[/code]

2) Your foreach loop makes no sense. You are using the key ($uID) for each item in your query. The keys will just be in the format 0, 1, 2, etc. You should have been using the values ($c) which will be the ids of the records.

3) You do not need to do several DELETE queries. You can do one query to delete multiple records. You just need to create your query in this manner:

DELETE FROM table WHERE uID IN ([i]commaseparatedlist[/i]).

You can convert the $_POST['delete'] array into a comma separated list using implode. So, just replace the foreach stament with this:

[code]<?php
if (isse($_POST['delete'])) {
    $idList = (",", $_POST['delete']);
    $sql = "DELETE FROM my_search_table WHERE uID IN ($idList)";
    mysql_query($sql);
}
?>[/code]
Link to comment
Share on other sites

decided to go with a foreach loop. but it doesn't work either. i don't see anything wrong with it... even my debugging shows that there's nothing wrong with it... but it STILL won't work and i have NO idea why...
[code=php:0]
/*****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);
          }
[/code]

what's going on here?!
Link to comment
Share on other sites

Show an example of what the print_r() of the $_POST['delete'] is putting on the page.

Also, you should add an error handler to your query to see if there are any errors

[code]<?php
mysql_query($sql) OR die ("The query:<br>" . $sql . "<br>Caused the following error:<br>" . mysql_error());
?>[/code]

Is the uID a number field?
Link to comment
Share on other sites

yes, the uID IS a field in my_search_table.

here's what my HTML checkbox looks like...
[code]<TD align=center><input type=checkbox name=delete[] value=". $row['uID'] .">[/code]

here's my foreach loop...
[code=php]
foreach($_POST['delete'] as $k => $c)
{
//echo $k ." / ". $c ." || ";  //<-- prints the EXACT correct information for the sql query to be properly xecuted.
$sql = "DELETE * FROM my_search_table WHERE uID = '". $c ."'";
mysql_query($sql);
}
[/code]
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]
0 / 3ZFc7FmpYiGpyTybHKXix14teZsQlQ || 1 / FVWxsWALHwtzWjKX23NsMXwbjImaqh || 2 / pSWURh1EsraukzZwYHwiF4DHJnaB3B
[/code]

so, what i see is that it's passing the correct information to the foreach loop, but for some reason or another, my SQL query won't process the request, and i can't figure out why. can anybody help?
Link to comment
Share on other sites

Well, did you try adding the event handler to your query like I suggested? I will show you if any errors are occuring.

You can use the foreach if you want, but using the IN operator as I suggested above would be much more efficient since you would be doing a single query instead of many.
Link to comment
Share on other sites

alright, mjdamato, this is what it printed when i put the error handling statement in there.
[code]
0 / 7djtV0nIPaui9Wt7WkIUBts2M3b3bh || The query:
DELETE * FROM my_search_table WHERE uID = 7djtV0nIPaui9Wt7WkIUBts2M3b3bh
Caused the following error:
You have an error in your SQL syntax near '* FROM my_search_table WHERE uID = '7djtV0nIPaui9Wt7WkIUBts2M3b3bh'' at line 1[/code]
even if i click more than one checkbox, it only prints an error for the FIRST of the many to be checked (in order from top to bottom). which makes sense. but what does the error mean?
Link to comment
Share on other sites

IT WORKS IT FINALLY WORKS!!!!! it was the '*' and the single quotes in the SQL query that were causing all the problems!!!! IT WORKS!!!!
[code=php:0]
$_POST['delete'];
if($_POST['delete'] != NULL){
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) OR die ("The query:<br>" . $sql . "<br>Caused the following error:<br>" . mysql_error());
}
}
[/code]

RIGHTEOUS!!!!!! thank you guys so much!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.