PDA

View Full Version : Why wont this SQL Query work?



Jack!
28-11-2012, 03:19 PM
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

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.

Moved by Samm (Forum Moderator): From "Technology Discussion" as it is better suited here :)

Tomm
28-11-2012, 03:34 PM
UPDATE users SET ip='$ip' WHERE user='$username';

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

Jack!
28-11-2012, 03:38 PM
UPDATE users SET ip='$ip' WHERE user='$username';

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

Yep

Tomm
28-11-2012, 03:41 PM
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?


Yep

Jack!
28-11-2012, 03:49 PM
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.

Tomm
28-11-2012, 04:04 PM
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>


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.

Jack!
28-11-2012, 05:22 PM
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! :)

Want to hide these adverts? Register an account for free!