开发者

PHP verify if every $array[$key] is not empty

开发者 https://www.devze.com 2023-02-16 02:10 出处:网络
So, i have this code that seems to be doing the mechanical work correctly, avoiding to write on tables if one or more fields from a form are left empty, but the echos and messages aren\'t working as t

So, i have this code that seems to be doing the mechanical work correctly, avoiding to write on tables if one or more fields from a form are left empty, but the echos and messages aren't working as they supposed to:

SOLUTION TO THE PROBLEM: I don't believe this will be useful to anyone, but just for the record, the solution is simple:

$campos = array('nome','morada','email','telemovel','codigopostal','vat');
foreach ($campos as $key => $campo) {
    $campos[$key] = $_GET[$campo];
    if(!isset($_GET[$campo])|| empty($_GET[$campo])){
                header("Location: ../index.php?erro=".$campo);
                $verifica=FALSE;
                die();
            }else{
                $verifica=TRUE;
            }
}

This will give me some problems, not 开发者_如何学JAVAwhat i really wanted but solves the logical problems i was having. Thanks to you all guys.

$campos = array('nome','morada','email','telemovel','codigopostal','vat');
foreach ($campos as $key => $campo) {
    $campos[$key] = ($_GET[$campo]);
    while(list($key, $campo)= each($campos))
            if(!isset($_GET[$campo])|| $_GET[$campo]==""){
                echo("não preencheu um dos campos");
                $verifica = FALSE;
                die();
            }else{
        echo $_GET[$campo]." \r\n";}
        $verifica = TRUE;
}

if($verifica==TRUE){
some irrelevant code.
}

As i said, the code itself is working flawlessly, BUT if i only left 2 empty fields on the form, the echo $_GET[$campo] will be working even though the variable $verifica will be set as FALSE

AN HINT:

One of the fields is making the code to fail, is the second one, so, if i do this:

../phcexport.php?nome=myname&morada=&codigopostal=postcode&email=@.com&vat=12312­3&telemovel=00800

And ignore the second value from the array, the code will work like a charm, i can try as many combinations as i can as long as i left the second field empty, it will work properly, giving me the error "some field is empty", in this case i know it is, the "morada" is empty, and if i fill it in the code says "its ok, all filled in" BUT the "morada" should be the last to be filled so the code works. Funny... (I'm sorry about all the text to describe the problem, i'm Portuguese)

EDIT2: For the rest of the code i need to use $campos[$key], so attribute a key to the arrays is essencial.


The problem is that you reset $verifica in the else. So regardless if you set it once to FALSE, the last foreach iteration will determine the outcome. But you can "simplify" the whole approach to:

$verify = array_search(0, array_map("strlen", $campos)) === false;

This simply checks for the string length of each array entry. If none of them is 0, then the expression will return true for $verify;


instead of doing $_GET[$campo]=="", you can maybe use the empty function :

empty($_GET[$campo])

This should address all problems related to different data types.

Your also maybe running in a problem because you're using a variable named $campo in two different loops, try changing the variable name in the while loop to see if it works better.


Check the exact value of $campo that is causing the incorrect behaviour, then consult this table to make sure you are using the right kind of comparison

http://www.php.net/manual/en/types.comparisons.php#types.comparisions-loose


try following solution, empty fields can not pass the test

<?php
$campos = array('nome', 'morada', 'email', 'telemovel', 'codigopostal', 'vat');
$verifica = TRUE;
foreach ($campos as $campo) {
    if (!isset($_GET[$campo]) || empty($_GET[$campo])) {
        echo "não preencheu um dos campos" ;
        $verifica = FALSE;
        break;
    } else {
        echo $_GET[$campo] ."\r\n";
    }
}

if ($verifica == TRUE) {
//  some irrelevant code.
} else {
    die();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消