PHP Operator's not working

software development

#1

Hey all. I have a slight bug on my website. A number of my pages use a variety of operators in simple math functions. Just recently, JUST recently have they begun to act weird. For instance, when asking to add 1 it adds 2 instead. This goes for a number of different codes which are all pretty normal looking. Here’s some of the code that doesn’t work:

[quote]$lol1 = ($userdata[‘example’] * 25);
$add2 = ($userdata[‘user_example2’]);
$add3 = $add2 + $add;[/quote]

Okay, now this would seem pretty fine, right? Takes the number of whatever it is you have, multiply it by twenty five. Then when I do the query, it adds whatever the outcome is. WRONG. It doubles. So, say the sum of example * 25 = 100, it’ll add 200 to the user’s database. Here’s a second issue.

[quote]$addition = ($userdata[‘example3’] + 1);

$dbquery = “UPDATE “.$db_prefix.“users SET example3= ‘$addition’ WHERE user_id=’”.$userdata[‘user_id’].”’”;[/quote]

Again, instead of just adding one to the table, it adds two. And that’s all the codes consist of. I haven’t edited anything. Can anyone help because this is holding the release of my project back. :frowning:


#2

Am I right in saying that you are taking the data from, and writing back to, a database? If so, it is possible that this may be related to the data type of your table fields. Another thing you can do is to check that the data are of the correct type with the gettype() function.

si-blog
Max discount on any plan with promocode SCJESSEYTOTAL


#3

Huh, good idea. Some types may be varchar and others may be INT. Would this be an issue? Going to check right now.


#4

Well, I’m stumped. I changed around a few types to match. But this is obviously, now, a database issue. Considering I’ve echoed the variables on the same page where the query is run. It adds up properly through the code but the DB insertion doubles EVERYTHING. 1 becomes 2, 2 becomes 4 and so on and so forth. What is this deal?


#5

Have you tried echoing the queries themselves to make sure that the entire statements are right at the last possible instant before going to the database? What I would be inclined to do is something like this:

<?php function debug_mysql_query($q) { echo "$q\n"; return mysql_query($q); } ?>

Then, find and replace all calls to mysql_query() with debug_mysql_query() in order to make sure that there’s no chance of something going weird between writing and running the query. It’ll also point out if you’re running queries unexpectedly, which could also lead to problems.


#6

The function didn’t work. :\

I’m truly stumped. I’ve echoed the query and it shows what it should be doing but it isn’t LOL


#7

lol…

It’s working now. I didn’t touch a thing. :stuck_out_tongue:


#8

Haha. That is both awesome and really scary. But it’s hard to argue with problems that make no sense just suddenly going away! ^^;;


#9

I’m kind of believing it had something to do with me switching the skins on the site. I was using one when it wasn’t working. I went back to the default and everyone was able to do everything with no problems. So, I’m going to switch to the newer one and test it to see if it is the root. If so, I’ll just trash it. :slight_smile: