coderb Posted April 14, 2008 Share Posted April 14, 2008 Hi All, I'm trying to find the correct method for this small problem. I have an edit form that displays a mysql value (column type: float) in an input field. this is a price field that is displayed using this code: value="<?php echo number_format($rowSQL['price'],2)?>" so the price is displayed as eg: $ 1,984.56 now the user updates the price value to $2,000.00 and submits. (sometimes they may include the comma, sometimes not.) what is the correct way to save this value to the database? Do I need to use a php string function to remove the comma, or is there a setting that saves this as is to the db, correctly? Quote Link to comment https://forums.phpfreaks.com/topic/101023-converting-an-input-amount-to-valid-float-for-db-update/ Share on other sites More sharing options...
laffin Posted April 14, 2008 Share Posted April 14, 2008 prolly using str_replace wud be the easiest way about removing the commas Quote Link to comment https://forums.phpfreaks.com/topic/101023-converting-an-input-amount-to-valid-float-for-db-update/#findComment-516595 Share on other sites More sharing options...
coderb Posted April 14, 2008 Author Share Posted April 14, 2008 thanks for the info, thought there just might be a db setting or something that recognizes this as a number fomat, but I guess that since the comma is generated by a php string function it is purely for display, so a reverse function is necessary on db update. thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/101023-converting-an-input-amount-to-valid-float-for-db-update/#findComment-516643 Share on other sites More sharing options...
discomatt Posted April 14, 2008 Share Posted April 14, 2008 Yeah, it doesn't like any non float characters... You could also use filter var to get rid of any non-float characters. <?php $number="$2,500.00"; // Filter anything that isn't a number, +/-/. var_dump(filter_var($number, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION)); ?> Should return string(7) "2500.00" Quote Link to comment https://forums.phpfreaks.com/topic/101023-converting-an-input-amount-to-valid-float-for-db-update/#findComment-516668 Share on other sites More sharing options...
laffin Posted April 14, 2008 Share Posted April 14, 2008 LOL, love these forums. as other users show the different functions php has. Nice job discomatt Quote Link to comment https://forums.phpfreaks.com/topic/101023-converting-an-input-amount-to-valid-float-for-db-update/#findComment-517106 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.