Can't put IP-address in database
I'm trying to put an ip-address in my database. If I do an echo like this:
echo $_SERVER['REMOTE_ADDR'];
But if I'm trying to put it in a variable or in the database it gives nothing, so in my database it says: NULL The commands I used for that:
$ip = $_SERVER['REMOTE_ADDR'];
mysql_qu开发者_如何学Goery("UPDATE users SET last_ip='".$ip."' WHERE id=".$row['id']) or die(mysql_error());
I don't know what I'm doing wrong. Can someone help me with this please?
Thanks!
You will want to make your last_ip
column an int(10) unsigned
and then change your UPDATE
to be:
$SQL = "UPDATE users
SET last_ip = INET_ATON('$ip')
WHERE id='{$row['id']}'";
Then when selecting you would use:
$SQL = "SELECT INET_NTOA(last_ip) AS last_ip
FROM users";
This converts the IP address into an integer for efficient storage. For more information please see the MySQL manual pages for INET_ATON()
and INET_NTOA()
.
Otherwise if you want it to be stored as text rather than in the most efficient way you can set your last_ip
column to be char(16)
and continue to use the UPDATE
query you posted in your question.
Your code is correct and should work. Unless as commented by @wallyk, the data type of the ip field is unsupported one.
However, just to make sure wrap the WHERE condition in '
(Single Quote) and try.
精彩评论