Jump to content

Entering ips into a database with INET_ATON.


Namtip

Recommended Posts

So after surfing the net I found that the 'best' way to enter ip addresses into a database was by using the INET_ATON function because you can then put it into a unsigned interger column and that will save space. But after looking around I can't find a way of fitting it into php.

 

This is my attempt:

	$ip = getenv("REMOTE_ADDR");

	$query = sprintf("UPDATE INTO user (
		name, password)
	VALUES ('%s','%s','%s')",
mysql_real_escape_string($_SESSION['name']),
mysql_real_escape_string(md5($_SESSION['values']['password']))
INET_ATON('$ip')); //line 37

$result2 = mysql_query($query, $db) or die(mysql_error($db));

 

I get a parse error:

Parse error: syntax error, unexpected T_STRING in C:\x\xampp\htdocs\pages\login.php on line 37.

 

This worked until I wanted to add the ip into the database. Any help appreciated.

Link to comment
Share on other sites

To start with, you're trying to insert 3 values into 2 fields . . .

 

Thanks, that's a good spot, I'm always doing that.

 

and with an UPDATE clause  :-\

 

Sorry I should of said that it would get inserted in the registration process, then updated every time the user is logged in.

 

I'm worried that I'm not coding line 37 in the right way.

 

Link to comment
Share on other sites

The PHP error is likely being triggered by a missing comma after the value on the previous line.

 

UPDATE INTO isn't valid syntax. It's either:

 

UPDATE `table` SET `field` = 'value'

or

INSERT INTO `table` (`field`) VALUES ('value')

or

INSERT INTO `table` (`field`) VALUES ('value') ON DUPLICATE KEY UPDATE `table` SET `field` = 'value'

Link to comment
Share on other sites

$query = sprintf("INSERT INTO user (
		name, password, ip)
	VALUES ('%s','%s','%s')",
mysql_real_escape_string($_SESSION['name']),
mysql_real_escape_string(md5(SALTY . $_SESSION['values']['password'])),
INET_ATON($ip));

//let's run the query
$result = mysql_query($query, $db) or die(mysql_error($db));

 

Thanks Pikachu!

 

I've added a , to the end of the password line to get rid of the previous parse error. now I get a new error:

 

Fatal error: Call to undefined function INET_ATON() in C:\x\xampp\htdocs\p\form_process.php on line 37

 

How are you meant to code the function in?

Link to comment
Share on other sites

	$ip = getenv("REMOTE_ADDR");
	$ip12 = inet_pton($ip);

$query = sprintf("UPDATE user SET ip=%s WHERE name_id=%s",
mysql_real_escape_string($ip12),
mysql_real_escape_string($_SESSION['name_id']));

 

error message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 WHERE name_id=2' at line 1

 

Can you not mysql_real_escape_string the inet_pton function result? Does my code look okay?

Link to comment
Share on other sites

Yay! yeah, I got it working!

 

$query2 = sprintf("UPDATE user SET ip=%s WHERE name_id=%s",
mysql_real_escape_string($ip12),
mysql_real_escape_string($_SESSION['name_id']));

 

I already had a $query variable. You got to love how shameless I am in telling you that. Don't respect my skills or intelligence, do look up to and admire my determination!

Link to comment
Share on other sites

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.