문제

So I have a simple database exercise at school and I'm nearly finished but I don't know how to restrict values.

I have a database like:

ID | Name | Age
 1 | John | 20
 2 | Jack | 30

And so on.

Is there a way to prevent users from inserting an age lower than 18? I've been searching a lot and didn't find the answer... Thanks in advance!

도움이 되었습니까?

해결책

With PHP, you can validate the form with something like:

$enteredAge = 17; //from the user

$minAge = 18;
$maxAge = 125; //debatable

if( !in_array( $enteredAge, range( $minAge, $maxAge) ) ){
    //Incorrect age.
}

If you feel as though an array is too "expensive" for this type of thing:

$enteredAge = 17; //from the user

$minAge = 18;
$maxAge = 125; //debatable

if($enteredAge < $minAge || $enteredAge > $maxAge){
    //Incorrect age.
}

다른 팁

$min_age=18;

if($age>18)
{
    mysql_query("INSERT INTO database (name,age) VALUES ('$name','$age')");
}
else
{
    die("Sorry, but you are too young to register");
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top