简体   繁体   中英

Update DB column every time a link is clicked via PHP

I have searched online for this issue, but the results are mostly based around Excel.

I have a webpage that has a list of videos. Every time someone clicks on a link, they are taken to a separate page where they can view a video. I have a table in my mysql database that links video id to views. I would like to increment the views column every time someone clicks on the link.

I know how to do this on the database side, but I am trying to figure out how to trigger the DB operation via PHP. I thought about having a script at the top of the page that calls the stored procedure to update the tally everytime the page is loaded, but the problem with this is that the video doesn't start automatic ally when the page loads. Users have to manually click on "play".

Why not write an Update query on the table which gets called everytime a link is clicked?

It is a little confusing where you want to increment the counter. You give three situations:

  1. When a user clicks a link to a particular video's page
  2. When a user loads a particular video's page
  3. When a user clicks play on a video

1 & 2 wouldn't give an accurate count of those who actually watched the video, so I'll choose that you meant 3.

It depends on your video player then. If the player has a way to hook a javascript function to the start of a video, then you can use that to make an ajax request back to a server page that updates your database. If the player has no hooks then you're limited to on page load, which will be less reliable.

Does this help? If I miss understood the question, please clarify by updating the question or adding comments. PS. adding code of what you've tried goes a long away as well.

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