[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
Quote:
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;
Quote:
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();
}
}
Re: Looping through POST data going wrong
PHP Code:
$IP = explode(' ',$_POST['IP']); != $IP = explode('\n',$_POST['IP']);
Re: Looping through POST data going wrong
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
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 ;)
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, -1, PREG_SPLIT_OFFSET_CAPTURE);
echo $chars[0][0];
echo '<BR>';
echo $chars[1][0];
?>
to split by new lines (if anyone else wants it)