Search code examples
phpsqlif-statementstringisnumeric

PHP SQL Allow empty or numeric input field


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 :)


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 ";
    }