Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can PHP help me with this problem...

Status
Not open for further replies.

garethashton

Programmer
May 13, 2001
19
0
0
US
I connect to a MySQL database with PHP. On one of my forms, I need to get a money ammount into my database so I can make calculations on it.
If I enter 2000 then it stores it in a number field as 2000.
But if I enter 2,000 then it stores it as 2.

Is there a field type in MySQL that can store a number (not a string) but also regcognise a comma (,) as a 1000's place divider and not a period (.) Is it also possible to enter a curreny symbol ($)?

--Or can PHP remove non-digit signs and still give the correct number to MySQL?
Any help is appreciated.
 
Try this:

[tt]
$number = "2,000";
$number = str_replace (",", "",$number);
# continue with your insert query
[/tt]

Hope this helps.
-Vic
vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
hi
u can check the input by user thru some client side script like javascript if it is a number by isNaN() or something
and then proceed with your queries.
but it is always safe to use server side script in these matters
:)
spookie
 
Thanks to both vcherubini and spookie, your suggestions were of help to me. My only question to vcherubini is if I am understanding this script right, can I also do this to remove the "$" sign:

$number = str_replace (",", "",$number);
$number = str_replace ("$", "",$number);
 
heck I belive you can even do

str_replace (",", "",str_replace ("$", "",$number));

first removes the $ then removes the , from that returned result. :) not nessary, but just thought it would be fun to point out. Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top