Page 1 of 2

Adding points to an existing accout???

Posted: Sun Mar 14, 2004 3:57 pm
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)

Posted: Sun Mar 14, 2004 4:03 pm
by tim

Code: Select all

<?php
$sql="UPDATE MEMBERS SET points=points + 25 WHERE username='$_SESSION["username"]'";
?>

Posted: Sun Mar 14, 2004 4:04 pm
by Illusionist
you might want to use UPDATE instead of INSERT

EDIT: I was too slow this time!!

Posted: Sun Mar 14, 2004 4:14 pm
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)

Posted: Sun Mar 14, 2004 4:15 pm
by Illusionist
well what is line 121??

Posted: Sun Mar 14, 2004 4:16 pm
by Joe
$sql1 = "UPDATE members SET points = points + 25 WHERE username = '$_SESSION['username']'";

Thats line 121!!!

Posted: Sun Mar 14, 2004 4:21 pm
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

Posted: Sun Mar 14, 2004 4:39 pm
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)

Posted: Sun Mar 14, 2004 4:43 pm
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

Posted: Sun Mar 14, 2004 4:48 pm
by Illusionist
nope. it should actually be

Code: Select all

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

Posted: Sun Mar 14, 2004 4:56 pm
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

Posted: Sun Mar 14, 2004 4:59 pm
by Illusionist
try mysql_query!!

Posted: Sun Mar 14, 2004 5:01 pm
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.

Posted: Sun Mar 14, 2004 5:02 pm
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)

Posted: Sun Mar 14, 2004 5:06 pm
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)