Jump to content

(PHP+MySQL) Not submitting data correctly after splitting (exploding)


Recommended Posts

Hey, I'm using the following lines of code to get the submitted IP(s) and place them in a database

$UID = randomkey(10).'-'.randomkey(10);

$counterMax =  substr_count($_POST['IP'],"\r\n");


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

$sql = mysql_connect($GLOBALS["dbhost"], $GLOBALS["dbuser"], $GLOBALS["dbpass"]);
$dbHandle = mysql_select_db($GLOBALS["dbname"]);

$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']);

$IP = preg_split("/[\n,]+/", $_POST['IP'], -1, PREG_SPLIT_OFFSET_CAPTURE);
$NewIP = str_replace(' ','',mysql_real_escape_string($IP[$counter][0]));
$NewIP = $NewIP."\r"; //I tried the \r part, but it doesnt make and difference and i've tried without the /r ect

$sql = "INSERT INTO `$DB` ( `IP` , `Email` , `Reason` , `Log` , `spare` , `Uid` )
VALUES (
'$NewIP', '$EMAIL', '$REASON', '$LOG', '$SPARE' ,'$UID'
);"; 

$result = mysql_query($sql);

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

}

however, when i try to find a certain IP

$result = mysql_query("SELECT * FROM `tempbase` WHERE `IP` = '".$_GET['IP']."';", $sql);

it doesnt find it, and when i edit the database under phpmyadmin it ends up giving me somthing like

UPDATE `tempbase` SET `IP` = '123.123.123.1' WHERE CONVERT( `tempbase`.`IP` USING utf8 ) = '123.123.123.1\r' AND CONVERT( `tempbase`.`Email` USING utf8 ) = '' AND CONVERT( `tempbase`.`Reason` USING utf8 ) = '' AND CONVERT( `tempbase`.`Log` USING utf8 ) = 'Temp_Logs/February-4-2007-8-21-00-am.' AND CONVERT( `tempbase`.`spare` USING utf8 ) = '219-90-252-115.ip.adam.com.au' AND CONVERT( `tempbase`.`Uid` USING utf8 ) = 'WBCSNRMUHF-CYAWRNMKWO' LIMIT 1 ;

and then when i try to find the IP using the PHP as above, it works.

I'm guessing that this has something to do with the encoding and the \r at the end of the IP field, how can i implement that when i submit the data using the code as posted above?

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.