Jump to content

MySql problem


YoDevil

Recommended Posts

Hi, I have this code and it is not working and I don't know why!

<?PHP
include 'conf/conn.php';

$username = $_POST['username'];
(int)$points = $_POST['points'];     //This is 2
$actualpoints_query = "SELECT points FROM users WHERE 'username'='$username'";   //This should be 1
$actualpoints =  mysql_query($actualpoints_query);
(int)$newpoints = $points + $actualpoints;   //This should be 3 but it is 6!

$ins = mysql_query("UPDATE users SET points = '$newpoints' WHERE 'username' = '$username'");
if ($ins)
	die ((string)$newpoints);   //This says me 6
else
	die ("Error: " . mysql_error());   //I'm not getting any errors
?>

The int points is 2, and the points actually in my username field are 1, but the result for newpoints is 6 and it doesn't update the field!

Can someone help me, please? :)

Link to comment
https://forums.phpfreaks.com/topic/283548-mysql-problem/
Share on other sites

The first part of your code should be

$username = mysql_real_escape_string($_POST['username']);
$points = (int)$_POST['points'];     //This is 2

$actualpoints_query = "SELECT points FROM users WHERE 'username'='$username'";   
$actualpoints_result =  mysql_query($actualpoints_query);
$row = mysql_fetch_assoc($actualpoints_result);             // fetch the returned row
$actualpoints = $row['points'];         //This should be 1
$newpoints = $points + $actualpoints;   //This should be 3 but it is 6!

However that was unnecessary - all you need is an update query

$username = mysql_real_escape_string($_POST['username']); // protect against SQL injection
$points = (int)$_POST['points']; //This is 2

mysql_query("UPDATE users SET points = points + $points WHERE 'username'='$username' ");
Link to comment
https://forums.phpfreaks.com/topic/283548-mysql-problem/#findComment-1456644
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.