Using commas in MySQL Decimal fields

I am having problems with entering a price into a MySQL decimal (10,2) field.  If a person enters 2118.56 in the HTML form, the record enters OK.  However, if they enter 2,118.56, the price field in the record is 2.00.

I am using PHP to insert the record.

Should I be using addslashes, or something else?

Thanks,

Calvin
Calvin ClosePresidentAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Raynard7Connect With a Mentor Commented:
If you are using php then you would need to use preg_replace() in php to remove all characters that are not equal to 0-9 or . or -
http://au.php.net/manual/en/function.preg-replace.php

mysql's fields will only accept plain numbers - commas are not acceptable in a numeric type
0
 
Aamir SaeedCommented:
you can strip of comma first and then enter into the DB
0
 
Aamir SaeedConnect With a Mentor Commented:
for example

$value = '2,118.56';
$value = str_replace(',', '', $value);
echo $value;
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
Aamir SaeedCommented:
also try using this in query

   REPLACE(field, ',' , '')
0
 
Raynard7Commented:
i_m_aamir I agree, but if the user then does something different, like spaces in the number then you would need to do more str_replaces, if you use a regular expression then it will remove everything that is incorrect and you will not get this problem,

alternatively you could just test to see if the number fits the correct format - ie ########.## and if not then just post an error and get the user to correct it themselves.
0
 
Aamir SaeedCommented:
you might try something like

$my_text="This is my text, there is a comma,\rblah blah\nblah blah\ttabbed in here\tand here\n";

function replace_chars($input)
{
      $filter=array(",","\r","\n","\t");
     
      return str_replace($filter,"",$input);
}

print replace_chars($my_text);
0
All Courses

From novice to tech pro — start learning today.