Forum Moderators: coopster

Message Too Old, No Replies

Session variables and mySql insert

         

cyclic

6:06 pm on Feb 7, 2005 (gmt 0)

10+ Year Member



Hi

I have initiated a session variable at log-on that holds the user's user name which is his/hers email address. I want to use this user name from the session variable to insert data to the correct user in the db. How do I write the sql query using a WHERE statement for the session variable?

i.e. INSERT into client(name,surname) values ('$name','$surname') WHERE email = (this is where I'm lost!)

jatar_k

6:12 pm on Feb 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it would be something like

$sql = "INSERT into client(name,surname) values ('$name','$surname') WHERE email = '" . $_SESSION['username'] . "'";

that would depend on the name of the session var into which you have stored the email address

cyclic

6:33 pm on Feb 7, 2005 (gmt 0)

10+ Year Member



Thanks for the speedy response. I have tried an insert without the WHERE statement and a new record is created. Once I put the WHERE statement in using the session variable nothing is inserted even though there is a matching email address in one record. I have started the session so it should be available.

supermanjnk

6:45 pm on Feb 7, 2005 (gmt 0)

10+ Year Member



Use UPDATE instead of insert, if you are going to be adding to a record that already exists.

example:

UPDATE table_name
SET column_name = new_value
WHERE column_name = some_value

cyclic

7:18 pm on Feb 7, 2005 (gmt 0)

10+ Year Member



Still no luck here, I think it must be my sql for the insert as I can insert a new record ok. It is so long since I have done this I must be missing something obvious although I am not getting any syntax errors.

supermanjnk

7:47 pm on Feb 7, 2005 (gmt 0)

10+ Year Member



$sql ="UPDATE Person SET name = '$name', $surname = '$surname' WHERE email = '" . $_SESSION['username'] . "'";

this is how the query should look.

jatar_k

8:22 pm on Feb 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sorry cyclic, my fault, I didn't even pay attention to the fact it said insert, not update. I should've caught that.

cyclic

8:32 pm on Feb 7, 2005 (gmt 0)

10+ Year Member



Thanks to the both of you. I feel a bit of an idiot re. insert/update. I hope it will slowly come back to me - all working nicely thanks.