2

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_query("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!

Ed Harper
  • 21,127
  • 4
  • 54
  • 80
Bjorn Seigers
  • 159
  • 4
  • 12

2 Answers2

4

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.

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • Hey! Thanks for the reply. I tried your SQL statement and I changed the type in my database, but still nothing is put in the database. Really strange.. Should it be a server-error? EDIT: even when I change it to CHAR(16) it doesn't work.. – Bjorn Seigers May 11 '11 at 09:36
  • And if you `var_dump($ip);` before the `mysql_query` what do you get? – Treffynnon May 11 '11 at 09:44
  • Still nothing. I also asked it to some people here and nobody knows. It should be right like this. I don't get it.. – Bjorn Seigers May 11 '11 at 11:17
  • OK so there is problem further up in your code. Sounds like perhaps the variable `$ip` is being overridden somewhere. `var_dump` `$ip` and `$_SERVER['REMOTE_ADDR']` to verify the data they are carrying. – Treffynnon May 11 '11 at 11:31
  • I tried the var_dump and somehow there still is no value in the $ip variable.. And the weird thing is, I can do an echo and print the ip-address, but when I print the $ip variable it says it's empty.. – Bjorn Seigers May 11 '11 at 18:09
  • `var_dump` both variables as I mentioned above and paste them both here otherwise no one will be able to help you. I suspect that you need to trace the source of the IP back until you find the disconnect. – Treffynnon May 12 '11 at 08:24
  • I'm not sure what you meant, but I did this: And all I get is: "no ip-address"NULL – Bjorn Seigers May 12 '11 at 09:16
  • `` as I said a few comments ago. Then __immediately__ after your `$ip = $_SERVER['REMOTE_ADDR'];` do `var_dump($ip);`. Keep var_dumping the variables until you find where it is ending up `NULL`. – Treffynnon May 12 '11 at 09:49
  • If you don't mind I'll hare you my login-page. I'm still learning PHP and I do appreciate it that you want to help me. But I not always understand what you mean. http://paste.bradleygill.com/index.php?paste_id=286792 – Bjorn Seigers May 12 '11 at 10:09
  • Try the following code: http://paste.bradleygill.com/index.php?paste_id=286795 I have added the `var_dumps` in for you. – Treffynnon May 12 '11 at 10:44
  • Thanks a lot for the edit in my code ;) I replaced it with my code. And I tried to log in and see if there was an IP-address in the database, but there isn't one. I also get the message (from my own echo) that the $ip-variable is empty. – Bjorn Seigers May 12 '11 at 11:23
  • What about the var_dumps I added. What did they print to screen? `` is the most important one to see that IP address is actually available to PHP. – Treffynnon May 12 '11 at 12:13
  • I can't see something new is printed on the screen, so I suppose it doesn't work? If I just do 'echo $_SERVER['REMOTE_ADDR'];' Then I get the ip, but not in the variable – Bjorn Seigers May 12 '11 at 13:43
  • Sorry, but I am not able to debug it any further without that information. I cannot see anything untoward with the code so I wouldn't know where to go from here. – Treffynnon May 12 '11 at 13:44
  • No problem! Thanks for the help. I just didn't use it, it was for a check if there was going to happen something bad on the website. – Bjorn Seigers May 17 '11 at 15:29
1

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.

Starx
  • 77,474
  • 47
  • 185
  • 261