Question

I'm having troubles getting this if statement to work properly. I wan't the POST field "reference_nr" to be able to contain an empty value or numeric value. This is my code:

    if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$_POST['faktura_email'])||
    !is_numeric($_POST['skadesnummer'])||
    !is_numeric($_POST['faktura_aar'])||
    (strlen($_POST['faktura_aar']) > 4)||
    !is_numeric($_POST['faktura_nr'])||
    (strlen($_POST['faktura_nr']) > 3)||
    !is_numeric($_POST['debitor'])||
    !is_numeric($_POST['reference_nr'])){

        print "Query fail";

    } else {

        print "Query success";

    }

I'll hope you understand my question. Have a nice day :)

Was it helpful?

Solution

you you want it to be a numeric is_numeric() doing the job ,if you want it empty !isset() doing it so :

if( is_numeric($_POST['reference_nr']) || isset($_POST['reference_nr'])){
    echo "referece_nr is numeric or empty";
 }else{
   echo "it's not ";
}

OTHER TIPS

You should check to see if the parameters are set before you try accessing them. You can use

if(isset($_POST['param_name']))
  $val = $_POST['param_name'];
else
  $val = null;

Then do any other data validation on the variables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top