Adding points to an existing accout???

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Adding points to an existing accout???

Post by Joe »

I have been trying to develop a script which adds 25 points into the users account if they manage to complete a challenge. I have no clue on how my script below doesn't work. It is just a little part from what I have got but it should be enough to explain the situation:

if (mysql_num_rows($result))
{
$row = mysql_fetch_assoc($result);
$points = $row['points'] + 25;
mysql_query("INSERT INTO members (points) VALUES ('$points') WHERE username = '".$_SESSION['username']."'");
echo "<center><h3><b>Well Done ".$_SESSION['username']."</b><h3>Application 1 Complete</center>";
mysql_close($link);

Can anyone please help me?

Regards



Joe 8)
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

Code: Select all

<?php
$sql="UPDATE MEMBERS SET points=points + 25 WHERE username='$_SESSION["username"]'";
?>
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

you might want to use UPDATE instead of INSERT

EDIT: I was too slow this time!!
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Nope that just gives me an error saying:

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /home/.sites/27/site244/web/scripts/app_up.php on line 121


Regards


Joe 8)
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

well what is line 121??
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

$sql1 = "UPDATE members SET points = points + 25 WHERE username = '$_SESSION['username']'";

Thats line 121!!!
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

ooooh! ya LOL... the problem is points + 25, you can't do that in an SQL statment like that. First get teh points back and then add 25 to them then update the db....

Code: Select all

$row = mysql_fetch_assoc($result);
$points = $row['points'] + 25;
$sql1 = "UPDATE members SET points =$points WHERE username = '$_SESSION['username']'";
try that
MrBurns
Forum Newbie
Posts: 5
Joined: Sun Mar 14, 2004 3:14 pm

Post by MrBurns »

Surely

Code: Select all

$sql1 = "UPDATE members SET points =$points WHERE username = '$_SESSION&#1111;'username']'";
should be

Code: Select all

$sql1 = "UPDATE members SET points =$points WHERE username = '$_SESSION&#1111;''username'']'";
or something similar (i think thats right i havent used PHP for a long while)
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Nah that don't work neither. Here is the actual code. Its very messy I know but it will be cleaned up soon.

if (strlen($app1) <> 0)
{
if ($app1 != "eclipse")
{
echo "Sorry, you have entered the wrong password!";
echo "<p>";
}
else
{
if ($app1 == "eclipse")
{
$link = mysql_connect("???", "???", "???");
mysql_select_db("connex") or die("Could not connect!" . mysql_error());
$sql = "SELECT points FROM members WHERE SesID = '".$_SESSION['username']."'";
$result = mysql_query($sql) or die(mysql_error());

if (mysql_num_rows($result))
{
$row = mysql_fetch_assoc($result);
$points_up = $row['points'] + 25;
mysql_query("INSERT INTO members points = $points_up WHERE SesID = '$_SESSION['username']'") or die("Could not connect!");
echo "<center><h3><b>Well Done ".$_SESSION['username']."</b><h3>Application 1 Complete</center>";
mysql_close($link);
}
}
}

Regards


Joe
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

nope. it should actually be

Code: Select all

$sql1 = "UPDATE members SET points=$points WHERE username='".$_SESSION['username']."'";
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Now i get this error:

Fatal error: Call to undefined function: sql_query() in /home/.sites/27/site244/web/scripts/app_up.php on line 121
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

try mysql_query!!
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

Illusionist wrote:ooooh! ya LOL... the problem is points + 25, you can't do that in an SQL statment like that.
if you go to my website you'll see the counter at the bottom of the page

the math part is controlled soley by that: (actual code):

Code: Select all

<?php
$sql =  "UPDATE counter SET count =  count + 1" or die (mysql_error());
$result = mysql_query($sql) or die (mysql_error());
?>
adds 1 to the field, perhaps theres something with there WHERE clause that is messing it up, but works for me so you can in fact do that.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Yeah ok I got that one. Just a silly mistake. The only prob now is that the points are not even adding. I am using an INT variable for the points column. Mabey thats the probem???


Regards


Joe 8)
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

well with the attritube INT it can hold a number to 2147483647

I assume the points arent exceding that limit???? (silly question I know but got to ask)
Post Reply