sharpcode Posted February 1, 2013 Share Posted February 1, 2013 mysql_query("UPDATE videos SET views = $views WHERE code = '$video'"); I need it to be called but only once a session per user. At moment if i refresh page it updates. Anyone able to assist me? First time i really am going to be using sessions now. Quote Link to comment https://forums.phpfreaks.com/topic/273892-sessions-and-update-sql/ Share on other sites More sharing options...
Psycho Posted February 1, 2013 Share Posted February 1, 2013 Assuming you start the session at the beginning of your script(s) if(!isset($_SESSION['viewed'])) { mysql_query("UPDATE videos SET views = $views WHERE code = '$video'"); $_SESSION['viewed'] = 1; } Quote Link to comment https://forums.phpfreaks.com/topic/273892-sessions-and-update-sql/#findComment-1409430 Share on other sites More sharing options...
sharpcode Posted February 1, 2013 Author Share Posted February 1, 2013 Do i have to start the session and end the session on every page of my site? Quote Link to comment https://forums.phpfreaks.com/topic/273892-sessions-and-update-sql/#findComment-1409431 Share on other sites More sharing options...
Zane Posted February 1, 2013 Share Posted February 1, 2013 Do i have to start the session and end the session on every page of my site? Yes you do. Except for the last part. You do not have to end the session. Sessions end when the browser is shut down. Quote Link to comment https://forums.phpfreaks.com/topic/273892-sessions-and-update-sql/#findComment-1409432 Share on other sites More sharing options...
sharpcode Posted February 1, 2013 Author Share Posted February 1, 2013 [b]Warning[/b]: session_start() [[url="http://dontstopdreaming.net/function.session-start"]function.session-start[/url]]: Cannot send session cookie - headers already sent by (output started at /home/public_html/video.php:5) in [b]/home/public_html/video.php[/b] on line [b]6[/b] [b]Warning[/b]: session_start() [[url="http://dontstopdreaming.net/function.session-start"]function.session-start[/url]]: Cannot send session cache limiter - headers already sent (output started at /home/public_html/video.php:5) in [b]/home/public_html/video.php[/b] on line [b]6[/b] its the first line as well... any ideas? looking at tutorials now and it seems i am doing it right and have no extra lines or anything... Quote Link to comment https://forums.phpfreaks.com/topic/273892-sessions-and-update-sql/#findComment-1409434 Share on other sites More sharing options...
Psycho Posted February 1, 2013 Share Posted February 1, 2013 And where is the code? Are you sure there are no spaces or line breaks before the opening PHP tags and that it is not included in another file that outputs content before it is included? Quote Link to comment https://forums.phpfreaks.com/topic/273892-sessions-and-update-sql/#findComment-1409439 Share on other sites More sharing options...
sharpcode Posted February 1, 2013 Author Share Posted February 1, 2013 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <?php session_start(); $video = $_REQUEST['code']; error_reporting(E_ALL); //..... then rest of code thats the top of the file Quote Link to comment https://forums.phpfreaks.com/topic/273892-sessions-and-update-sql/#findComment-1409442 Share on other sites More sharing options...
Psycho Posted February 1, 2013 Share Posted February 1, 2013 Everything before "<?php" is output to the browser. You cannot have any output before you start the session. Quote Link to comment https://forums.phpfreaks.com/topic/273892-sessions-and-update-sql/#findComment-1409451 Share on other sites More sharing options...
sharpcode Posted February 1, 2013 Author Share Posted February 1, 2013 (edited) if(!isset($_SESSION['viewed'])) { $views = $views + 1; mysql_query("UPDATE videos SET views = $views WHERE code = '$video'"); $_SESSION['viewed'] = 1; } Still seems to not be working. In the database i will check what one of the video pages is and say its 13 i will view it and it will still print 13. Then i refresh twice or so and it will turn into 15 and then wont change after that. Then once i move to another video page same thing. Edit: Putting session_start at the very start of the file worked by the way thanks Psycho. Edited February 1, 2013 by sharpcode Quote Link to comment https://forums.phpfreaks.com/topic/273892-sessions-and-update-sql/#findComment-1409476 Share on other sites More sharing options...
sharpcode Posted February 1, 2013 Author Share Posted February 1, 2013 Also how do i make the sessionname a variable like $code? because i am using the counter on other pages for other videos so it needs to be dynamic.. $_SESSION['.$title.'] didn't seem to work or even $_SESSION['title'] Quote Link to comment https://forums.phpfreaks.com/topic/273892-sessions-and-update-sql/#findComment-1409482 Share on other sites More sharing options...
sharpcode Posted February 1, 2013 Author Share Posted February 1, 2013 (edited) sorry for this post but couldn't edit either of my last ones fro some reason... anyway i have done the following instead... $views = $row['views']; if(!session_is_registered($title)) { $views++; mysql_query("UPDATE videos SET views = $views' WHERE code = '$video'"); session_register($title); } echo $views; it seems to work except the first time i view the page it will update right but moment i go back or refresh it will return to the original value and stay that way. So a bit closer but also stranger edit: fixed... notive the ' after $views? well yeh i just did lol -.- Edited February 1, 2013 by sharpcode Quote Link to comment https://forums.phpfreaks.com/topic/273892-sessions-and-update-sql/#findComment-1409495 Share on other sites More sharing options...
Zane Posted February 1, 2013 Share Posted February 1, 2013 (edited) A better idea would be to add the extra view in your SQL update query UPDATE videos SET views = views + 1 WHERE ..... ...NOTE.. and yes, there is no $view variable in that query. All that query will do is increment the views field by one. Edited February 1, 2013 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/273892-sessions-and-update-sql/#findComment-1409546 Share on other sites More sharing options...
sharpcode Posted February 13, 2013 Author Share Posted February 13, 2013 (edited) remarked as not solved because its playing up again... if(!session_is_registered($title)) { mysql_query("UPDATE videos SET views = views + 1 WHERE code = '$video'"); session_register($title); } not sure what the problem is and title is a variable edit: problem is like in the past doesn't update the view when i click on the page. But then when i refresh will skip any number (so far has been from 3 or even as high as 6) Edited February 13, 2013 by sharpcode Quote Link to comment https://forums.phpfreaks.com/topic/273892-sessions-and-update-sql/#findComment-1412157 Share on other sites More sharing options...
sharpcode Posted February 14, 2013 Author Share Posted February 14, 2013 Still cant seem to work it out. if i remove the ' ' around $video it doesnt update at all then... Quote Link to comment https://forums.phpfreaks.com/topic/273892-sessions-and-update-sql/#findComment-1412430 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.