Jump to content

My first request! Calculation with strings?


Mathy

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Well, "echo $sqla;" echoes "8' WHERE uname='Mathy'".

 

So, if $l['points'], $l['gold'] and $l['posts'] was 1, it should instead echo:

 

"UPDATE users SET points='3', gold='6', posts='2' WHERE uname='Mathy'"

 

 

Why does it break it all up?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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;
}
?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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)."', ....").

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.