Results 1 to 6 of 6

Thread: [RESOLVED] Looping through POST data going wrong

  1. #1

    Thread Starter
    Addicted Member Jazz00006's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Resolved [RESOLVED] Looping through POST data going wrong

    Well, I have the code below, and its probably not very nice, but it works if someone submits only one line, such as

    123.123.123.1 123.123.123.2 123.123.123.3 123.123.123.4 123.123.123.5
    but if there are line breaks;
    123.123.123.1
    123.123.123.2
    123.123.123.3
    123.123.123.4
    123.123.123.5
    it doesnt work.

    It also skips the first explode from the post data.


    Can anyone help me with these two probelms, thanks

    Page for example( http://greyfyre.info/Submit.php )


    PHP Code:
    $counterMax =  substr_count($_POST['IP']," ");


    for ( 
    $counter 0$counter <= $counterMax$counter += 1) {

        
    $sql mysql_connect($GLOBALS["dbhost"], $GLOBALS["dbuser"], $GLOBALS["dbpass"]);
        
    $dbHandle mysql_select_db($GLOBALS["dbname"]);
        
    $IP explode(' ',$_POST['IP']); 
        
    $EMAIL mysql_real_escape_string($_POST['EMAIL']); 
        
    $REASON mysql_real_escape_string($_POST['REASON']); 
        
    $LOG mysql_real_escape_string($target_path); 
        
    $SPARE gethostbyaddr($_SERVER['REMOTE_ADDR']);
        
    $NewIP mysql_real_escape_string($IP[$counter]);
        
        
    $sql "INSERT INTO `$DB` ( `IP` , `Email` , `Reason` , `Log` , `spare` )
        VALUES (
        '
    $NewIP', '$EMAIL', '$REASON', '$LOG', '$SPARE'
        );"

        
        
    $result mysql_query($sql);
        
        
    mysql_close($sql);
        if (
    is_resource($rows)) {
              } else {
            echo 
    mysql_error();
            }
            


  2. #2
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    354

    Re: Looping through POST data going wrong

    PHP Code:
     $IP explode(' ',$_POST['IP']);   !=  $IP explode('\n',$_POST['IP']); 

  3. #3

    Thread Starter
    Addicted Member Jazz00006's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: Looping through POST data going wrong

    doesnt work.

  4. #4
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    354

    Re: Looping through POST data going wrong

    Quote Originally Posted by Jazz00006
    doesnt work.
    i know...i assumed you knew the difference between a whitespace and a newline.

    it isn't code.

    it is a statement that means you need to explode '\n' which is a newline.
    ' ' != '\n'. When you explode ' ' you are only exploding whitespace is ascii 0x20 and newline is like 0xA. In other words if you hit enter after each ip address you are putting in '\n' so when you explode ' ' it fails because '\n' != ' '

    Although even if you explode '\n' you aren't exploding newline.

    Look into
    http://us2.php.net/manual/en/function.preg-split.php
    Last edited by superbovine; Feb 3rd, 2007 at 10:45 PM.

  5. #5

    Thread Starter
    Addicted Member Jazz00006's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: Looping through POST data going wrong

    ah, you were being sneaky should have noticed

    ill try and fix up my code with this new info, cheers

  6. #6

    Thread Starter
    Addicted Member Jazz00006's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: Looping through POST data going wrong

    cheers, it ended up being something like

    PHP Code:
    <?PHP
    $str 
    'new
    line'
    ;
    $chars preg_split("/[\n,]+/"$str, -1PREG_SPLIT_OFFSET_CAPTURE);
    echo 
    $chars[0][0];
    echo 
    '<BR>';
    echo 
    $chars[1][0];
    ?>
    to split by new lines (if anyone else wants it)

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