Jump to content

Video Counter dependant on number of plays


MarlonM

Recommended Posts

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. 
 
Link to comment
Share on other sites

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);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.