Jump to content

[SOLVED] Session MySQL


yogibear

Recommended Posts

Hi all

 

I am quite new to php and am having a little problem with sessions. when a user logs in there username becomes a session and every page checks this session this all works fine, which is great but I would like it to search the database for this username this is part of the code that i have.

 

mysql_query("UPDATE userinformation SET credits = $c

WHERE username = ". $_SESSION['username']; mysql_close($con);

 

this is the error I am getting

Parse error: syntax error, unexpected ';' in

 

sorry if i have not been clear, I hope you can help, thanks in advanced

 

yogi

Link to comment
https://forums.phpfreaks.com/topic/51554-solved-session-mysql/
Share on other sites

Hi thanks for your fast reply, ;D I thought everything was working when I got no error but then I noticed that it has stopped the calculation from working. This is all my work in progress code

 

<?
// You may copy this PHP section to the top of file which needs to access after login.
session_start(); // Use session variable on this page. This function must put on the top of page.
if(!session_is_registered("username")){ // if session variable "username" does not exist.
header("location:index.php"); // Re-direct to index.php
}

$con = mysql_connect('localhost','***','***');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());

  }mysql_select_db("***", $con);

$result = mysql_query("SELECT * FROM userinformation
WHERE username='yogibear'");

while($row = mysql_fetch_array($result))

$a = $row['credits'];

$b = 5000;
$c = $a + $b;

mysql_query("UPDATE userinformation SET credits = $c WHERE username = ". $_SESSION['username']");
mysql_close($con);

?>
[code]

The code is meant to take credits and add 5000 to it and then replace the original amount of credits with the total, but it doesn’t, it does when ". $_SESSION['username']"); is replaced with 'yogibear'");

Any ideas

Thanks

[/code]

Link to comment
https://forums.phpfreaks.com/topic/51554-solved-session-mysql/#findComment-254423
Share on other sites

if this is your exact code you are missing a period after the $_session variable

 

mysql_query("UPDATE userinformation SET credits = $c WHERE username = '". $_SESSION['username']."');


 

you have ".$_SESSION['username']")

 

if that doesn't work, make sure you have your sessions set.

 

EDIT:

Also you need single quotes around the session variable before the double quotes.

Link to comment
https://forums.phpfreaks.com/topic/51554-solved-session-mysql/#findComment-254453
Share on other sites

Hi now im getting an error

Parse error: syntax error, unexpected $end in /home/sites/bf2c.co.uk/public_html/orderexhaust.php on line 27

This is all the code

 

<?
// You may copy this PHP section to the top of file which needs to access after login.
session_start(); // Use session variable on this page. This function must put on the top of page.
if(!session_is_registered("username")){ // if session variable "username" does not exist.
header("location:index.php"); // Re-direct to index.php
}

$con = mysql_connect('localhost','***','***');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());

  }mysql_select_db("***", $con);

$result = mysql_query("SELECT * FROM userinformation
WHERE username='yogibear'");

while($row = mysql_fetch_array($result))

$a = $row['credits'];

$b = 5000;
$c = $a + $b;

mysql_query("UPDATE userinformation SET credits = $c WHERE username = '". $_SESSION['username']."');
mysql_close($con);
?>

 

Thanks for your help

Link to comment
https://forums.phpfreaks.com/topic/51554-solved-session-mysql/#findComment-254520
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.