Jump to content


Photo

Sessions and update sql


  • Please log in to reply
13 replies to this topic

#1 sharpcode

sharpcode

    Member

  • Members
  • PipPip
  • 15 posts

Posted 31 January 2013 - 10:41 PM

 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.

#2 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,708 posts
  • LocationCanada

Posted 31 January 2013 - 11:38 PM

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

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#3 sharpcode

sharpcode

    Member

  • Members
  • PipPip
  • 15 posts

Posted 31 January 2013 - 11:39 PM

Do i have to start the session and end the session on every page of my site?

#4 Zane

Zane

    Advanced Member

  • Moderators
  • 3,818 posts
  • LocationMurphy, NC

Posted 31 January 2013 - 11:52 PM

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.
btn_donate_SM.gif  Want to thank me?  Contribute to my PayPal piggy-bank
 
401299.png

#5 sharpcode

sharpcode

    Member

  • Members
  • PipPip
  • 15 posts

Posted 01 February 2013 - 12:06 AM

[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...

#6 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,708 posts
  • LocationCanada

Posted 01 February 2013 - 12:46 AM

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?
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#7 sharpcode

sharpcode

    Member

  • Members
  • PipPip
  • 15 posts

Posted 01 February 2013 - 01:21 AM

<!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

#8 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,708 posts
  • LocationCanada

Posted 01 February 2013 - 02:07 AM

Everything before "<?php" is output to the browser. You cannot have any output before you start the session.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#9 sharpcode

sharpcode

    Member

  • Members
  • PipPip
  • 15 posts

Posted 01 February 2013 - 05:51 AM

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 by sharpcode, 01 February 2013 - 05:53 AM.


#10 sharpcode

sharpcode

    Member

  • Members
  • PipPip
  • 15 posts

Posted 01 February 2013 - 06:55 AM

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']


#11 sharpcode

sharpcode

    Member

  • Members
  • PipPip
  • 15 posts

Posted 01 February 2013 - 08:54 AM

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 by sharpcode, 01 February 2013 - 08:57 AM.


#12 Zane

Zane

    Advanced Member

  • Moderators
  • 3,818 posts
  • LocationMurphy, NC

Posted 01 February 2013 - 01:54 PM

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 by Zane, 01 February 2013 - 01:55 PM.

btn_donate_SM.gif  Want to thank me?  Contribute to my PayPal piggy-bank
 
401299.png

#13 sharpcode

sharpcode

    Member

  • Members
  • PipPip
  • 15 posts

Posted 12 February 2013 - 10:29 PM

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 by sharpcode, 12 February 2013 - 10:40 PM.


#14 sharpcode

sharpcode

    Member

  • Members
  • PipPip
  • 15 posts

Posted 14 February 2013 - 09:47 AM

Still cant seem to work it out. if i remove the ' ' around $video it doesnt update at all then...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com