Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Location
    UK
    Posts
    2,470
    Tokens
    2,975

    Latest Awards:

    Default Why wont this SQL Query work?

    Basically, when someone logs in I am trying to grab the IP, and then insert it in to the database..

    This is the code I have, Its not giving any errors but nothing is being inserted into the database..

    PHP Code:
    <?php
     
    include("config.php");

    $ip $_SERVER["REMOTE_ADDR"]; 
    $username mysql_escape_string($_SESSION["username"]); 
    mysql_query("UPDATE ip FROM `users` SET IP = '$ip' WHERE user='$username'"); 

    }
    ?>
    Probably something simple, Never tried grabbing the IP and putting it in a database before though.

    moderator alert Moved by Samm (Forum Moderator): From "Technology Discussion" as it is better suited here
    Last edited by efq; 29-11-2012 at 02:14 PM.

  2. #2
    Join Date
    Jun 2005
    Posts
    4,795
    Tokens
    0

    Latest Awards:

    Default

    UPDATE users SET ip='$ip' WHERE user='$username';

    assuming your users table has a column called ip and a column called user.

  3. #3
    Join Date
    Jul 2007
    Location
    UK
    Posts
    2,470
    Tokens
    2,975

    Latest Awards:

    Default

    Quote Originally Posted by Tomm View Post
    UPDATE users SET ip='$ip' WHERE user='$username';

    assuming your users table has a column called ip and a column called user.
    Yep

  4. #4
    Join Date
    Jun 2005
    Posts
    4,795
    Tokens
    0

    Latest Awards:

    Default

    Yep? Yes the query works or yes you do have a users table with a column called ip and a column called user?

    Edit: Also I assume the random } is just due to the copy and paste and is not actually left like that?

    Quote Originally Posted by Jack! View Post
    Yep

  5. #5
    Join Date
    Jul 2007
    Location
    UK
    Posts
    2,470
    Tokens
    2,975

    Latest Awards:

    Default

    Quote Originally Posted by Tomm View Post
    Yep? Yes the query works or yes you do have a users table with a column called ip and a column called user?

    Edit: Also I assume the random } is just due to the copy and paste and is not actually left like that?
    Oh hang on.

    I have the column IP and Username, But I'm only trying to update the IP Column, The Users was there to select the table (as the table is called users) but I'm not sure if that is needed or even if I was doing it right.

    And the } was there because I was in the process of adding something else below but never got around to it, my bad.

  6. #6
    Join Date
    Jun 2005
    Posts
    4,795
    Tokens
    0

    Latest Awards:

    Default

    UPDATE users SET ip='$ip' WHERE username='$username';

    Not directly related to your question but as a precaution whenever I use an update statement and I know I want to only change a single row I always add LIMIT 1 to the end of the statement. Like so:

    UPDATE users SET ip='$ip' WHERE username='$username' LIMIT 1;

    This is because by default if you don't specify a WHERE clause then all the rows will be altered. Also if the WHERE clause has a wacky value for whatever reason then you might end up altering more rows than you actually want. By adding the LIMIT 1 then you limit the potential damage to a single column rather than damaging all the data in a table if something goes wrong.

    Also here is the simplified synax for an update statement with a where clause:

    UPDATE <table name> SET <column you want to update>=<new value> WHERE <column to search in>=<value to match>

    For multiple columns you seperate the columns you want to update with commas:

    UPDATE <table name> SET <column you want to update>=<new value>, <another column you want to update>=<another value>,(...etc...) WHERE <column to search in>=<value to match>

    Quote Originally Posted by Jack! View Post
    Oh hang on.

    I have the column IP and Username, But I'm only trying to update the IP Column, The Users was there to select the table (as the table is called users) but I'm not sure if that is needed or even if I was doing it right.

    And the } was there because I was in the process of adding something else below but never got around to it, my bad.

  7. #7
    Join Date
    Jul 2007
    Location
    UK
    Posts
    2,470
    Tokens
    2,975

    Latest Awards:

    Default

    Quote Originally Posted by Tomm View Post
    UPDATE users SET ip='$ip' WHERE username='$username';

    Not directly related to your question but as a precaution whenever I use an update statement and I know I want to only change a single row I always add LIMIT 1 to the end of the statement. Like so:

    UPDATE users SET ip='$ip' WHERE username='$username' LIMIT 1;

    This is because by default if you don't specify a WHERE clause then all the rows will be altered. Also if the WHERE clause has a wacky value for whatever reason then you might end up altering more rows than you actually want. By adding the LIMIT 1 then you limit the potential damage to a single column rather than damaging all the data in a table if something goes wrong.

    Also here is the simplified synax for an update statement with a where clause:

    UPDATE <table name> SET <column you want to update>=<new value> WHERE <column to search in>=<value to match>

    For multiple columns you seperate the columns you want to update with commas:

    UPDATE <table name> SET <column you want to update>=<new value>, <another column you want to update>=<another value>,(...etc...) WHERE <column to search in>=<value to match>
    That works, but I assume I have something wrong in the database, as it only shows the first 5 digits and no dots in between them either.

    EDIT: Fixed it, thanks for the help!
    Last edited by Jack!; 28-11-2012 at 05:25 PM.

Posting Permissions

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