MarlonM Posted June 18, 2015 Share Posted June 18, 2015 I am creating a website where various users can upload videos as well as view them. Below is what I have done so far but it updates all the video with the same count and updates the DB with the ID and hits but fails to update the videoid. Anyone have any ideas. <?php $getViews = mysql_query("SELECT `hits` FROM `views` WHERE `id` = $video_id"); if(mysql_num_rows($getViews)) { $result = mysql_fetch_assoc($getViews); $videoViews = $result['hits']+1; } else { $videoViews = 1; } mysql_query("INSERT INTO `views` (`id`) VALUES ($video_id) ON DUPLICATE KEY UPDATE hits = hits+1"); echo 'Total Views: '.number_format($videoViews); ?> My DB is called views and the columns within are ID, hits and videoid. Quote Link to comment https://forums.phpfreaks.com/topic/296904-video-counter-dependant-on-number-of-plays/ Share on other sites More sharing options...
QuickOldCar Posted June 19, 2015 Share Posted June 19, 2015 Are you using upper or lower case for id? From what I see you only need 2 columns ID and hits, since you seem to be trying to use the $video_id variable as id. If ID is an autoincrement would not work as you expected. ditch the autoincrement on ID if have it and drop the videoid column $getViews = mysql_query("SELECT `hits` FROM `views` WHERE `ID` = $video_id"); if(mysql_num_rows($getViews)) { $result = mysql_fetch_assoc($getViews); $videoViews = $result['hits']+1; } else { $videoViews = 1; } mysql_query("INSERT INTO `views` (ID,hits) VALUES ('{$video_id}','1') ON DUPLICATE KEY UPDATE hits = hits+1"); echo 'Total Views: '.number_format($videoViews); Quote Link to comment https://forums.phpfreaks.com/topic/296904-video-counter-dependant-on-number-of-plays/#findComment-1514352 Share on other sites More sharing options...
QuickOldCar Posted June 19, 2015 Share Posted June 19, 2015 (edited) Was another post I did about tracking clicks or views plus by dates. Note it's using mysqli_ functions instead of the deprecated mysql_ http://forums.phpfreaks.com/topic/296442-unique-click-tracking-using-php/?do=findComment&comment=1512504 Edited June 19, 2015 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/296904-video-counter-dependant-on-number-of-plays/#findComment-1514353 Share on other sites More sharing options...
MarlonM Posted June 24, 2015 Author Share Posted June 24, 2015 Hi, I gave that a go but it still updates all the videos with the same hit count. Quote Link to comment https://forums.phpfreaks.com/topic/296904-video-counter-dependant-on-number-of-plays/#findComment-1514833 Share on other sites More sharing options...
QuickOldCar Posted June 26, 2015 Share Posted June 26, 2015 Could you post your current code and database structure, also any code how you are implementing this. Quote Link to comment https://forums.phpfreaks.com/topic/296904-video-counter-dependant-on-number-of-plays/#findComment-1515022 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.