Results 1 to 5 of 5

Thread: [RESOLVED] ForEach error

  1. #1

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Resolved [RESOLVED] ForEach error

    i am using a foreach to delete messages according to their id, stored in an array. It is from a form checkbox called "delete". Here is my code:

    PHP Code:
    $delete = array();
    $delete $_POST['delete'];
    foreach(
    $delete as $messid) {
            
    //$delete1 is a $_GET...
        
    if ($delete1!="delete") {
            
    #MOVE TO TRASH CAN
            
    $sql "UPDATE `messages` SET folder='trash' WHERE mess_id='$messid'";
            
    $query mysql_query($sql);
            echo 
    "Messages Was Sent To Trash Can!";
        } else {
            
    #DELETE MESSAGE
            
    $sql "DELETE FROM `messages` WHERE mess_id='$messid'";
            
    $query mysql_query($sql);
            echo 
    "Message Was Deleted!";
        }

    i have also tried:

    PHP Code:
    $delete = array($_POST['delete']; 
    the error is:

    Code:
    Warning: Invalid argument supplied for foreach() in /home/jphendr/public_html/includes/delete_message.php on line 26
    Line 26 is where foreach() is.
    My usual boring signature: Something

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: ForEach error

    $_POST['delete'] is not an array. This is why you get the warning. Use print_r to check the variable is an array.

    You are NOT escaping your variables before insetring them into SQL queries. I am sure I have mentioned the importance of this to you before.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: ForEach error

    i was using {} then i sent it to my friend to see if he could fix it and he sent it back like that. I forgot to put back the {}.

    If i select 2 checkboxes, and I use Print_r is only shows one of the selected checkboxes

    EDIT:

    can you only have one number in an array?
    My usual boring signature: Something

  4. #4

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: ForEach error

    ah i fixed it!

    on the form the checkbox was named "delete[]" and i was doing $_POST['delete[]'] instead of $_POST['delete']

    problem solved
    My usual boring signature: Something

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] ForEach error

    Quote Originally Posted by dclamp
    i was using {} then i sent it to my friend to see if he could fix it and he sent it back like that. I forgot to put back the {}.
    You are still not escaping the variable contents in any case. If someone sent a POST request with a value like "' OR true" they could delete all the records in the table. You must at least escape the data and should really use parameterised queries (using a data access library such as mysqli, MDB2 or PDO).

    If you are relying on magic quotes to escape the POST data for you then that is another problem which will cause you other issues. Magic quotes should always be off.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width