PHP Web Host - Quality Web Hosting For All PHP Applications Sign up for PayPal and start accepting credit card payments instantly
  Login or Register
 • Home • Downloads • Your Account • Forums • 

View next topic
View previous topic


Google
 
Web RavenPHPScripts (This Site)
Post new topic   Reply to topic
Author Message
Donovan
Client


Joined: Oct 07, 2003
Posts: 691
Location: Ohio

PostPosted: Thu Nov 29, 2007 9:28 am Reply with quote Back to top

Is there any preferred way to validate decimal values before writing them to the db?

Currently I have..

$irat_wt = $_POST['irat_wt'];
$grat_wt = $_POST['grat_wt'];
$appex_wt = $_POST['appex_wt'];

but all _POST values are a string aren't they?

These variable will hold the values of what weights of test results for different exam.

irat = individual readiness test
grat = group readiness test
appex = application exercise

Right now they are all data type decimal (3,2).

They will vote at the beginning of each year and decide how much the irat, grat, and appex is worth to their overall grade.

I was using values such as irat = 0.20, grat = 0.40, appex = 0.40

If I edit these values I want to ensure I validate the data before I update the table.

I guess I can decrease the length of the data type to 2,2 so I only have values such as .20 and .40.

Will intval() suffice?
View user's profile Send private message Visit poster's website ICQ Number
gotcha
Regular
Regular


Joined: Mar 14, 2005
Posts: 79

PostPosted: Thu Nov 29, 2007 10:06 am Reply with quote Back to top

intval will turn it into a whole number, removing the decimal point. I think the function you want to look at is number_format().
View user's profile Send private message Visit poster's website
evaders99
Moderator


Joined: Apr 30, 2004
Posts: 2853

PostPosted: Thu Nov 29, 2007 2:04 pm Reply with quote Back to top

floatval may also be a function to take a look at, depending on what you want to do
View user's profile Send private message Visit poster's website
montego
Site Admin


Joined: Aug 29, 2004
Posts: 7487
Location: Arizona

PostPosted: Fri Nov 30, 2007 10:08 pm Reply with quote Back to top

Raven tends to recommend the PHP
Only registered users can see links on this board!
Get registered or login to the forums!
. Some reasons why:

"It should be noted that ctype functions are always preferred over regular expressions, and even to some equivalent str_* and is_* functions. This is because of the fact that ctype uses a native C library and thus processes significantly faster."

I could have sworn too that they are "rock solid", but I cannot recall the reference.
View user's profile Send private message Visit poster's website
Gremmie
Former Moderator in Good Standing


Joined: Apr 06, 2006
Posts: 2401
Location: Iowa, USA

PostPosted: Fri Nov 30, 2007 10:19 pm Reply with quote Back to top

Unfortunately there is no ctype function for floating point numbers. Sad

If you are trying to prevent SQL injection and you know the value should be a float, I would use floatval() like evaders suggests. That will turn the string into a float, or 0 if it isn't a float. Then you could use sprintf to format it the way you want for the SQL query.

Code:

$x = sprintf('%5.2f', floatval($x));
$sql = "UPDATE ........... SET something = $x";
View user's profile Send private message
montego
Site Admin


Joined: Aug 29, 2004
Posts: 7487
Location: Arizona

PostPosted: Sat Dec 01, 2007 9:07 am Reply with quote Back to top

Ah, yes, sorry. Embarassed That is a bummer Sad
View user's profile Send private message Visit poster's website
Display posts from previous:       
Post new topic   Reply to topic

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Forums ©
 

All logos and trademarks in this site are property of their respective owner.
The comments are property of their posters, all the rest © 2002-2008 by Raven
Proud to be listed at Lobo Links Web Directory

You can syndicate our news using the file xml

CSE HTML Validator Helped Clean up This Page! [Valid RSS] valid RSS 2.0 Valid robots.txt Stop Spam Harvesters, Join Project Honey Pot

Website engines core code is © copyright by PHP-Nuke but has been heavily patched and modified by myself and others.
PHP-Nuke is a free software released under the GNU/GPL.


:: fisubice phpbb2 style by Daz :: PHP-Nuke theme by www.nukemods.com ::

:: fisubice Theme Recoded To 100% W3C CSS & HTML 4.01 Transitional Compliance by Raven and 64bitguy ::

:: W3C CSS Compliance Validation :: W3C HTML 4.01 Transitional Compliance Validation ::

zerosum