I'm running some PHP that queries regularly from a MySQL DB.
In my DB I have a decimal (5,4) field with an assortment of values, some of them real integers and some of them decimal numbers.
I'd like to take an <input type="text">
tag from a form, $_GET
the number the user entered, then compare it to a value in the DB.
Which of the three is the best way to go about this:
$_GET['user_value]
variable to float, then compare to DB value.$_GET['user_value]
variable to integer, then compare to DB value.$_GET['user_value]
to strings, and then compare the two.BTW: I need all the information after the decimal point.
MY CODE - $user_answer is the user's input, $correct_answer is DB Value
<?php
switch ($grid_type)
{
//If Single: Just Match Two Correct Answers
case "single":
if($user_answer === $correct_answer)
{
$is_correct = 1;
}
else
{
$is_correct = 0;
}
break;
//If Multiple: Check to see if user_answer Matches any of correct_answers
case "multiple":
if($user_answer === $correct_answer OR $correct_answer_2 OR $correct_answer_3 OR $correct_answer_4 OR $correct_answer_5)
{
$is_correct = 1;
}
else
{
$is_correct = 0;
}
break;
//If range: Check if user_answer is between the two range values
case "range":
if($user_answer > $correct_range_bottom AND $user_answer < $correct_range_top)
{
$is_correct = 1;
}
else
{
$is_correct = 0;
}
break;
default:
"An Error Has Occured - All Of Your Base Are Belong To Us.";
}
?>
@Tom None of the solution you listed are good. What you want to do is this:
get whatever value the user entered (using POST/GET request - post is prefered) then
$user_value = $_POST['user_value'];
if(is_numeric(str_replace('.','',$user_value))){
$user_value = sprintf('%0.4f',$user_value);
echo $user_value;
}else{
echo 'Wrong number entered';
}
then you compare with the value from the DB.