Problem with some code

So I am trying to implement a points system on my site where the user opts in for points then I choose the number of points to give out. While this works fine with just 1 user, when there are multiple users it adds to the user on the bottom of the list and sets the other users points to that. The code controlling the points is

[php]$query = mysql_query(“SELECT points FROM users WHERE points_list=1”){
WHILE ($rows = mysql_fetch_array($query));
$points = $_POST[‘points’];
$oldpoints = $rows[‘points’];
$newpoints = $oldpoints + $points;
mysql_query(“UPDATE users SET points=’$newpoints’ WHERE points_list=‘1’”)or die(“Update not successful” . mysql_error());
}[/php]

I cannot stress enough the importance of NOT using mysql_query, take a look yourself on PHP’s website, this feature will be dropped from PHP soon meaning any bit of your code with database queries as mysql_query will NOT WORK.

Then what should I use instead?

Butter1484, You can use this as a reference: http://php.net/manual/en/function.mysql-query.php

Thanks for that. I believe I will use mysqli. I have begun recoding for it. I will see what happens when I get to that part of the code.

Your problem is that you have different users but you are updating all of their points so that everyone will have the same number of points as the last user. Try this (I assume that the primary key in the user table is id):

$query = mysql_query("SELECT points, id FROM users WHERE points_list=1"){
WHILE ($rows = mysql_fetch_array($query));
$points = $_POST['points'];
$oldpoints = $rows['points'];
$userId = $rows['id'];
$newpoints = $oldpoints + $points;
mysql_query("UPDATE users SET `points`='$newpoints' WHERE `id`='$userId' AND `points_list`='1'")or die("Update not successful" . mysql_error());
}

With that I get syntax error, unexpected ‘{’ I would assume I would just replace the { with a ; and get rid of the corresponding } but how would that effect the rest of the code?

I think @masked just made a small type-o is all. Below should be syntax correct.

[php]$query = mysql_query(“SELECT points, id FROM users WHERE points_list=1”)
WHILE ($rows = mysql_fetch_array($query)){
$points = $_POST[‘points’];
$oldpoints = $rows[‘points’];
$userId = $rows[‘id’];
$newpoints = $oldpoints + $points;
mysql_query(“UPDATE users SET points=’$newpoints’ WHERE id=’$userId’ AND points_list=‘1’”)or die(“Update not successful” . mysql_error());
}[/php]

That works, but when I try to change it to mysqli it says it can’t use the database.

@Butter1484

The mysqli extension are not enabled by default… Read through the installation and configuration documentation located here: http://www.php.net/manual/en/mysqli.setup.php

My host did have it enabled. I found the problem to be that I needed to include $link in my query

Sponsor our Newsletter | Privacy Policy | Terms of Service