SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Do I have to identify with primary key?

    I have a loginscript where I want to update how many times a user have logged in when they login. My problem is that I want to update a cell in a row where I don't call the primary key (ID), but another cellrow called username. Is it possible to identify the row without calling the primary key?
    PHP Code:
    mysql_query("UPDATE Users SET 
    logins=logins+1 WHERE 
    username=
    $user"); 

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Of course. Have you tried it?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I have, it should work then? Perhaps I have done something wrong somewhere else, but it's strange becouse that code wont work, but when I try to do it where I identify the row by it's primarykey ID it works properly..

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try it in PHPMyAdmin - or, even better, MySQL Query Browser.

    I'm guessing your problem is syntax. You should enclose strings in quotes.
    PHP Code:
    mysql_query("UPDATE Users SET Logins = Logins + 1 WHERE Username = '{$user}'"); 
    Note that it's a good idea to enclose variables in curly braces inside double-quoted strings (Single quotes don't allow variable parsing) to separate string from variable.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! It was a syntax error I belive. I forgot to put ' ' around the variable.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Are your usernames guaranteed to be unique though?

    If they are then you don't need an id column ...

    If they aren't then you cannot count on this, you'll have to "update users set logins = login+1 where id = $id"

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good point, I might throw the ID column away then, since they will be unique

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Might depend on how many other tables re-use the id as an FK, but if its none, or only a few you might find it starts to clarify your sql statements, dereferencing all those id keys.


Bookmarks

Posting Permissions

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