简体   繁体   中英

Setting up a MySQL table to track user link clicks

This is a followup to a question I posted a few days ago.

basically, I have a site with six links. In order to access the site, users must log in using LDAP authentication. When they do this, I grab some of their account credentials (username, firstname, lastname), and store it in a PHP $_SESSION variable.

That works; the user can log in, and the session data is being stored successfully.

Now, I want to set up a way to track which links have been clicked by what users. Basically just store a time stamp in the database of when they clicked the link. I want to be able to see who has (or has not) clicked each link, and when.

Can I do this in a single table / would that be a bad idea? I was thinking setting up the table like this:

TABLE (each bullet indicative of a column)

  • auto-incrementing ID
  • user account name: abc1234
  • user account first name: John
  • link 1: Last Accessed 5/2/2012 at 4:15PM
  • link 2: NULL
  • link 3: NULL
  • link 4: Last Accessed 5/1/2012 at 2:20PM
  • link 5: NULL
  • link 6: NULL

basically the above would say that "John" had only clicked the first and 4th links. The rest are null because he has never accessed them. If he were to click #1 again, it would overwrite with the more recent date/time.

Can I do this in a single table? or will that create complications? I feel like the thing I will have the hardest time with is checking to see if the user is already in the database before adding the info (ie so that if John logs in a second time, a whole new row isn't created for him)

Thanks for any help!

That would be a bad idea. What if you wanted to have a seventh link? What if the user format would change?

This solution requires 3 tables:

  • Users - contains user data (And a user ID).
  • Links - contains link data (And a link ID).
  • Clicks - many-to-many relationship between users and links.

That third table would look like this:

user_id | link_id | timestamp
-----------------------------
1       | 2       | ...
2       | 2       | ...
1       | 3       | ...
............

why not just have

increment_ID
Account_ID
Link_URL
Timestamp

Then just insert a new record for each click. You also don't need to manage links since you'll store the entire URL path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM