Namtip Posted September 27, 2010 Share Posted September 27, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214533-entering-ips-into-a-database-with-inet_aton/ Share on other sites More sharing options...
Pikachu2000 Posted September 27, 2010 Share Posted September 27, 2010 To start with, you're trying to insert 3 values into 2 fields . . . Quote Link to comment https://forums.phpfreaks.com/topic/214533-entering-ips-into-a-database-with-inet_aton/#findComment-1116351 Share on other sites More sharing options...
Rifts Posted September 27, 2010 Share Posted September 27, 2010 nvm saw it Quote Link to comment https://forums.phpfreaks.com/topic/214533-entering-ips-into-a-database-with-inet_aton/#findComment-1116353 Share on other sites More sharing options...
mikosiko Posted September 27, 2010 Share Posted September 27, 2010 To start with, you're trying to insert 3 values into 2 fields . . . and with an UPDATE clause :-\ Quote Link to comment https://forums.phpfreaks.com/topic/214533-entering-ips-into-a-database-with-inet_aton/#findComment-1116358 Share on other sites More sharing options...
Namtip Posted September 27, 2010 Author Share Posted September 27, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214533-entering-ips-into-a-database-with-inet_aton/#findComment-1116377 Share on other sites More sharing options...
Pikachu2000 Posted September 27, 2010 Share Posted September 27, 2010 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' Quote Link to comment https://forums.phpfreaks.com/topic/214533-entering-ips-into-a-database-with-inet_aton/#findComment-1116381 Share on other sites More sharing options...
Namtip Posted September 27, 2010 Author Share Posted September 27, 2010 $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? Quote Link to comment https://forums.phpfreaks.com/topic/214533-entering-ips-into-a-database-with-inet_aton/#findComment-1116394 Share on other sites More sharing options...
Namtip Posted September 27, 2010 Author Share Posted September 27, 2010 Oh god, I've read in another post that the values are depreciated because of the introduction of IPv6 (gives us all more ips to use). I apparently should use 'inet_ntop() or inet_pton() instead'! http://beej.us/guide/bgnet/output/html/multipage/inet_ntoaman.html I'll post my new code in a sec. Quote Link to comment https://forums.phpfreaks.com/topic/214533-entering-ips-into-a-database-with-inet_aton/#findComment-1116403 Share on other sites More sharing options...
Namtip Posted September 29, 2010 Author Share Posted September 29, 2010 $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? Quote Link to comment https://forums.phpfreaks.com/topic/214533-entering-ips-into-a-database-with-inet_aton/#findComment-1116998 Share on other sites More sharing options...
Namtip Posted September 29, 2010 Author Share Posted September 29, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/214533-entering-ips-into-a-database-with-inet_aton/#findComment-1117326 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.