Jump to content

Recommended Posts

$sql="SELECT points, gold, posts FROM users WHERE uname='" . Decrypt_Text($UN) . "'";
$r = mysql_query($sql, $conn);
if(mysql_affected_rows()>0) {
   $l = mysql_fetch_array($r);
   $sqla="UPDATE users SET points='" . $l["points"] + 2 . "', gold='" . $l["gold"] + 5 . "', posts='" . $l["posts"] + 1 . "' WHERE uname='" . Decrypt_Text($UN) . "'";
   echo $sqla;
}

 

The code above echoes "8' WHERE uname='Mathy'"

 

What did I do wrong here? Isn't it possible to add numbers to strings?

What do you mean by add numbers to strings? If you want "this" + 1 to equal "this1", then you have to use the concatenator ".": "this".1 would equal "this1".  I would assume by your example, though, that you already know that, so please clarify your question.

my guess is that you're wrongly assuming what the $l array contains before running the calculations on it.  try running:

 

echo '<pre>'.print_r($l, TRUE);

 

just before the query to see what your values prior to the UPDATE are.

my guess is that you're wrongly assuming what the $l array contains before running the calculations on it.  try running:

 

echo '<pre>'.print_r($l, TRUE);

 

just before the query to see what your values prior to the UPDATE are.

I know it contains something! I am almost sure. However, I should look at it, you are right. I will be replying shortly.

<?php

$sql="SELECT `points`, `gold`, `posts` FROM `users` WHERE `uname`='" . Decrypt_Text($UN) . "'";

$r = mysql_query($sql, $conn);
if(mysql_affected_rows()>0) {
$l = mysql_fetch_array($r);

$po=$l["points"]+2;
$g=$l["gold"]+5;
$p=$l["posts"]+1;

$po=$_POST['po'];
$g=$_POST['g'];
$p=$_POST['p'];

$sqla="UPDATE `users` SET `points`='$po' AND `gold`='$g' AND `posts`='$p' WHERE `uname`='" . Decrypt_Text($UN) . "'";
$result=mysql_query($sqla);
echo $sqla;
}
?>

try

$sqla="UPDATE users SET points='" . ($l["points"] + 2) . "', gold='" . ($l["gold"] + 5) . "', posts='" . ($l["posts"] + 1) . "' WHERE uname='" . Decrypt_Text($UN) . "'";

i try to explain

PHP convert number to string and string to number, operator + except numbers and operator . except strings

old code say

"UPDATE users SET points='" . $l["points"] produce something like "UPDATE users SET points='5" type strimg

"UPDATE users SET points='5" + 2  convert "UPDATE users SET points='5" to number produce 0 and add 2 result is 2 (number)

2. "', gold='" . $l["gold"] => "2, gold='3" (string)

"2gold='3" + 5 (1st "2gold='3" to number => 2) 2 + 5 = 7

etc

 

So it's a problem of parentheses and operator precedence, then. Good call, sasa. In that case you could either use the nice clean solution laid out by redarrow or just put your addition in parentheses and concatenate (like "UPDATE users set points = '".($l['points'] + 2)."', ....").

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.