Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Sep 2009
    Location
    uk
    Posts
    83
    Thanks
    4
    Thanked 3 Times in 3 Posts

    cant get ip address to insert

    i have tried a few ways and can not get the ip address to insert in database.
    added all this to register file
    PHP Code:
    $ip getip();
    $ip explode(".",$ip);
    $data['ip']         = $user->escape(post('ip')); 
    this is my ip function
    PHP Code:
    ///////////////////////////////////////////GET IP

    function getip()
    {
        if (
    getenv('HTTP_X_FORWARDED_FOR'))
        {
          
    $ip=getenv('HTTP_X_FORWARDED_FOR');
        }
        else
        {
          
    $ip=getenv('REMOTE_ADDR');
        }
        return 
    $ip;

    and this is my insert code
    PHP Code:
    $sql "INSERT INTO `user` (
                                    `username` ,`ip` ,`password` ,`day` ,`month` ,
                                    `year` ,`gender` ,`country` ,`email` ,
                                    `referrer` ,`about` ,`confirmation`
                                    )
                                    VALUES (
                                    '{$username}', '{$ip}',  '{$password}', '{$day}', '{$month}',
                                    '{$year}', '{$gender}', '{$country}', '{$email}',
                                    '{$referrer}', '{$about}', '{$confirmation}')"

    the last code/ file also has
    PHP Code:
    $ip getip();
    $ip explode(".",$ip); 
    anyone help me.

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    what type of field are you trying to insert it into? also do you get any errors?

  • Users who have thanked angst for this post:

    nclemale (03-02-2010)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    While the datatype is very much important, I'm curious about this one as well:
    PHP Code:
    $ip getip(); 
    $ip explode(".",$ip); 
    Assuming that is used prior to the insertion, you now have $ip as an array of 4 items or one (depending on if IPv6 support is available or not). I doubt you'll need the explode call on there as well, the ip address is a string; if you want to convert it to a number, use the ip2long and long2ip functions.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    New Coder
    Join Date
    Sep 2009
    Location
    uk
    Posts
    83
    Thanks
    4
    Thanked 3 Times in 3 Posts
    no no errors. every other field enters the value thats inputted except the ip .

    i tried

    `ip` varchar(20) NOT NULL DEFAULT '',
    have also tried
    `ip` int(20) NOT NULL DEFAULT '',

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by nclemale View Post
    no no errors. every other field enters the value thats inputted except the ip .

    i tried

    `ip` varchar(20) NOT NULL DEFAULT '',
    have also tried
    `ip` int(20) NOT NULL DEFAULT '',
    You may have missed mine, see if its an array since that won't be insertable.
    As for you're datatype, make sure its a varchar(15) or char(15) at minimum. Don't use an integer unless you're planning on storing the long number, and sprintf it with a %u modifier to force an unsigned integer from PHP.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    well int. wont do it unless you convert using the ip2long() as Fou-Lu mentioned.

    but it does look like maybe your trying to insert an array.

    just out of curiosity, why are you exploding the ip anyway?

  • #7
    New Coder
    Join Date
    Sep 2009
    Location
    uk
    Posts
    83
    Thanks
    4
    Thanked 3 Times in 3 Posts
    no idea . am not an expert .am just using some code off another script to make this ip insert into database .
    thought it may play a part in getting the info that i need to insert it .

  • #8
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ah ok,
    well remove the explode code, make your field a varchar ( as Fou-Lu said above ),
    and do a normal insert. that should do it

  • #9
    New Coder
    Join Date
    Sep 2009
    Location
    uk
    Posts
    83
    Thanks
    4
    Thanked 3 Times in 3 Posts
    now your confusing me. normal insert ? what other kind is there . ?

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by nclemale View Post
    now your confusing me. normal insert ? what other kind is there . ?
    O.o
    Just normal.
    The problem is that $ip is an array so its considered complex, something that a database cannot handle. You would need to flatten the complex data back into a string (or just not explode it at all), or serialize the data so its representable as a string.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #11
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    in short,
    just comment out/remove this code and test it:

    $ip = explode(".",$ip);
    $data['ip'] = $user->escape(post('ip'));

  • #12
    New Coder
    Join Date
    Sep 2009
    Location
    uk
    Posts
    83
    Thanks
    4
    Thanked 3 Times in 3 Posts
    right i have took explode out and changed the type to varchar(16).

    still dont work .

  • #13
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    try this;

    PHP Code:
    $sql "INSERT INTO `user` ( 
                                    `username` ,`ip` ,`password` ,`day` ,`month` , 
                                    `year` ,`gender` ,`country` ,`email` , 
                                    `referrer` ,`about` ,`confirmation` 
                                    ) 
                                    VALUES ( 
                                    '{$username}', '{$_SERVER['REMOTE_ADDR']}',  '{$password}', '{$day}', '{$month}', 
                                    '{$year}', '{$gender}', '{$country}', '{$email}', 
                                    '{$referrer}', '{$about}', '{$confirmation}')"

    if that fails, echo/print $sql and post the result.
    Last edited by angst; 03-02-2010 at 09:13 PM.

  • #14
    New Coder
    Join Date
    Sep 2009
    Location
    uk
    Posts
    83
    Thanks
    4
    Thanked 3 Times in 3 Posts
    $data['ip'] = $user->escape(post('ip'));
    is part of the register as i have the same for
    PHP Code:
    $data['username']     = $user->escape(post('username'));
        
    $data['password']     = $user->escape(post('password'));
        
    $data['cpassword']     = $user->escape(post('cpassword'));
        
    $data['day']         = $user->escape(post('day'));
        
    $data['month']         = $user->escape(post('month'));
        
    $data['year']         = $user->escape(post('year'));
        
    $data['gender']     = $user->escape(post('gender'));
        
    $data['country']     = $user->escape(post('country'));
        
    $data['email']         = $user->escape(post('email'));
        
    $data['referrer']     = $user->escape(post('referrer'));
        
    $data['about']         = $user->escape(post('about')); 
    it still dosnt work anyway with all those removed. as i say i have tried a few things before asking for help

  • #15
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    right, but can you post the echo'd $sql ?
    so we can see whats in the actual query being sent to mysql


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •