insert each time a user visits one of my pages

please tell me how to insert into a table

 create TABLE `last_visits` (
  `last_visits_id` int(11) NOT NULL auto_increment,  
  `profile_id` varchar(20) default NULL, -- name of user visiting page
  `this_user` varchar(20) default NULL, -- my current username
  `time` datetime      
  PRIMARY KEY  (`last_visits_id`)

I am doing inserts into table
if I run the php code multiple times by mistake I do not want to add new visits (because the user did not visit my page my page multiple times)

so maybe add a unique key
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
I think you want to check out this syntax:

and then, all you have to do is to specify the unique key to match what you consider "duplicate"
rgb192Author Commented:
I do not understand which columns to make unique

There is a small chance that one user visits both of my pages at the same exact time

  `this_user` varchar(20) default NULL, -- my current username
Kevin CrossChief Technology OfficerCommented:
I would handle this in a combination between application side and database. You could set a specific session variable that you store in the database for the specific visit. If the user accidentally posts back the same page, the same session variable will try to insert...if you make it unique the operation will fail.

In fact, you could have the INSERT code only execute if the referring page is not the current page itself.
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

rgb192Author Commented:
>>I would handle this in a combination between application side and database

I know I could do an insert into or replace into using php

how to handle it database side
Kevin CrossChief Technology OfficerCommented:
That was not what I was saying. I was saying use the PHP side to create a unique session token/value that you pass to the database. If each insert includes this token, then an accidental refresh will fail because you have that field set to a unique constraint.
rgb192Author Commented:
So how to only set that up using mysql

and then I will have another question about php
Kevin CrossChief Technology OfficerCommented:
For the MySQL side, add a column representing the session (unique visit ID). Create unique constraint on this column. Multiple inserts of the same key will fail.
Keeping unique constraint will create problem to your customer who would be visiting your website multiple times.


1> create DB with IP,COOKIE,userid( if you have login facility) , date, first time stamp,last time stamp
2> capture clients IP, cookie if presents its identification if it has been logged in, date.
   insert into your db all this value with time stamp in both columns
3> if client visits your page 1st time you could capture and save it in your db but if he visit other page just update last time stamp and if required you could even save page name with comma separated in new column .

If there is no record in whole day for record with same IP,COOKIE or user id it will insert or else it will update

I hope ALGO has been clear if any issue please do let us know.
Kevin CrossChief Technology OfficerCommented:
"Keeping unique constraint will create problem to your customer who would be visiting your website multiple times." < That is the point, sessions are unique to visit. Therefore, in the same visit, there should not be an insert for the same session. New visits would be a different session. If the Author captures userid information for the site, the IP and userid combination would work also; however, keep in mind that multiple users may originate from the same IP address, especially on corporate or Internet cafe networks, so IP only is not unique.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
Ip address not unique, so sessions can be unique

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.