Jump to content

MySql problem


YoDevil
Go to solution Solved by Barand,

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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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