Jump to content

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. 
 

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);

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 by QuickOldCar
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.