Jump to content

Recommended Posts

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?

 

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

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"

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.