CaseyC1 Posted December 24, 2007 Share Posted December 24, 2007 Somebody please pound the answer to this into this hard headed German. I am inputting several variables from a form and then updating a SQL db record with the information. I want to use three of these variables in a calculation. These variables are defined in the db as "Decimal 10,2". Here is the problem: If I use the variables as they are input (text strings I presume), my calculation does not work (i.e. a multiplication returns the value of 0). However, if I retrieve the values from the db after they have been stored and use the values (i.e. after fetch row $row - $row[my_variable]) in the multiplication everything works. I would like to use the values after they are input from the form and then store them and my calculation results into the db. I'm sure the problem is mixed mode multiplication. I have tried to convert the strings to decimal, but no luck. Any answers? Quote Link to comment https://forums.phpfreaks.com/topic/83042-form-input-data-vs-retrieved-from-db-data/ Share on other sites More sharing options...
wildteen88 Posted December 24, 2007 Share Posted December 24, 2007 post some code so we can see what you are doing. Not quite understanding you at the moment. Quote Link to comment https://forums.phpfreaks.com/topic/83042-form-input-data-vs-retrieved-from-db-data/#findComment-422389 Share on other sites More sharing options...
CaseyC1 Posted December 24, 2007 Author Share Posted December 24, 2007 Ok, as requested: $catalog_price = $db->mySQLSafe($_POST['catalog_price']); $myQuery1 = "SELECT markup FROM CubeCart_markup_table WHERE $catalog_price BETWEEN value_from and value_to"; $code = $db->mySQLSafe($_POST['productCode']); $myQuery2 = "SELECT my_price FROM CubeCart_inventory WHERE productCode = $code"; $myResult1 = mysql_query ($myQuery1); $myResult2 = mysql_query ($myQuery2); $row1 = mysql_fetch_array($myResult1, MYSQL_ASSOC); $row2 = mysql_fetch_array($myResult2, MYSQL_ASSOC); $my_price = $row2[my_price]; $markup = $row1[markup]; echo "My price:" .$my_price ."<br>"; echo "Markup:" .$markup ."<br>"; $retail = $my_price * $markup; echo "Retail:" .$retail ."<br>"; // STORE DATA IN CURRENT RECORD $record["markup"] = $markup; $record["price"] = $retail; The value "my_price" (as input from the html form) is available through "$my_price = $db->mySQLSafe($_POST['my_price']);" just as the value "catalog_price" is. I replaced that statement with the code highlighted in red to retrieve " my_price" from the db. The problem I run into is, when I assign the value of the variable $my_price with $my_price = $db->mySQLSafe($_POST['my_price']);" My multiplication returns a result of 0. If I run with the red code inserted the calculation works. The echo statements are just there to check the variables. There is a <br> at the end of each...that's why the statement looks wrong. Quote Link to comment https://forums.phpfreaks.com/topic/83042-form-input-data-vs-retrieved-from-db-data/#findComment-422399 Share on other sites More sharing options...
wildteen88 Posted December 24, 2007 Share Posted December 24, 2007 What does the above code output? Specificly this part: $my_price = $row2[my_price]; $markup = $row1[markup]; echo "My price:" .$my_price ."<br>"; echo "Markup:" .$markup ."<br>"; $retail = $my_price * $markup; echo "Retail:" .$retail ."<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/83042-form-input-data-vs-retrieved-from-db-data/#findComment-422467 Share on other sites More sharing options...
CaseyC1 Posted December 24, 2007 Author Share Posted December 24, 2007 The output looks from the code as listed above is like this: My price:11.94 Markup:2.05 Retail:24.477 When I change the code and use the variable as input through the form, however, the my_price variable looks different and my result is zero. I think the problem might be that there is a format mismatch. In the first example it looks like all three variables are actually numbers. In the example below isn't the My_price a text string? I don't know what php does with a mixed mode expression like this, but I know Fortran IV would throw up......lol My price:'11.94' Markup:2.05 Retail:0 The broader picture.....The script inputs about 23 items through a html form. All of the inputs are through text boxes even though some of the items are defined as decimals in the db. When I use the numbers as they were input through the form, I apparently get a text string. After writing the record to the db and then retrieving the numbers they are in the correct decimal format for the calculation. I would like to be able to convert the strings to a decimal so that I don't have to store the record and then immediately read it back to use the numbers. Quote Link to comment https://forums.phpfreaks.com/topic/83042-form-input-data-vs-retrieved-from-db-data/#findComment-422474 Share on other sites More sharing options...
wildteen88 Posted December 24, 2007 Share Posted December 24, 2007 What do you mean by: When I change the code and use the variable as input through the form, however the my_price variable looks different and my result is zero: How does $my_price now contain quotes when you do that? Quote Link to comment https://forums.phpfreaks.com/topic/83042-form-input-data-vs-retrieved-from-db-data/#findComment-422475 Share on other sites More sharing options...
CaseyC1 Posted December 24, 2007 Author Share Posted December 24, 2007 I added an explanation to the previous post. You were too quick for me with your post...... Quote Link to comment https://forums.phpfreaks.com/topic/83042-form-input-data-vs-retrieved-from-db-data/#findComment-422481 Share on other sites More sharing options...
wildteen88 Posted December 26, 2007 Share Posted December 26, 2007 Looks like you are storing your numbers as strings that is why when you echo $my_price you're getting '1323456' (quotes around the number). How is the form being populated? Post some more code here. Quote Link to comment https://forums.phpfreaks.com/topic/83042-form-input-data-vs-retrieved-from-db-data/#findComment-423501 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.