Some problem with foreach

Discussion in 'PHP' started by Grumps, Sep 16, 2007.

  1. #1
    I have a list of array that im getting from $_POST.

    instead the traditional way to strip_tags
    
    $name = strip_tags($_POST[name]);
    $name = mysql_escape_string($name);
    $name = ucfirst($name);
    $prize = strip_tags($_POST[prize]);
    $prize = mysql_escape_string($prize);
    $deposit = strip_tags($_POST[deposit]);
    $deposit = mysql_escape_string($deposit);
    $bonus = strip_tags($_POST[bonus]);
    $bonus = mysql_escape_string($bonus);
    
    PHP:
    I have planned to use foreach to run a loop trough those submitted array.

    
    foreach ($_POST as $value) {
    $value = strip_tags($value);
    $value = mysql_escape_string($value);
    }
    extract($value);
    
    PHP:
    i doubt my foreach loop is done correctly but it should give you the idea of what im trying to archive.
     
    Grumps, Sep 16, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    if (isset($_POST) AND is_array($_POST))
    {
        $_POST = array_map('strip_tags', $_POST);
        $_POST = array_map('mysql_real_escape_string', $_POST);
    
        extract($_POST);
    }
    
    PHP:

    This should do what you want.
     
    nico_swd, Sep 16, 2007 IP
  3. Grumps

    Grumps Peon

    Messages:
    592
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Absolutely, thanks alot for that..i'll be reading about array_map
     
    Grumps, Sep 16, 2007 IP
  4. Aron Schatz

    Aron Schatz Peon

    Messages:
    201
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It is also that you weren't operating on the elements in the array, just a new variable.

    You would need to replace the variable in the $_POST array for your array walk to work.
     
    Aron Schatz, Sep 17, 2007 IP