To doing a form in php and need to update the DB if a user make changes to the data [closed]

0

I know how to do this for if else, but I have 8 fields where you can have only 1,2,3 ... blank fields and I have to check if the field is blank and if it is not updating in DB, I wanted to know if you have the easiest way.

<form  action="alteraroficioC1.php" method="post" enctype="multipart/form-data">
            <tr><th>Nº</th><td><?php echo $idget ?></td><td>Alterar para: </td><td></td></tr>
            <tr><th>Data</th><td><?php echo $dataof ?></td><td><input type="input" name="data" id="<?php echo $idInput; ?>" onkeyup="mascaraData(<?php echo $idInput; ?>);" maxlength="10" placeholder="dd/mm/aaaa" /></td><td><input type="image" src="../images/save_01_16x16.png" name="id" value="<?php echo $manid; ?>"></td></tr>
            <tr><th>Solicitado por</th><td><?php echo $solicitado ?></td><td><input type="text" name="nomePor" </td><td><input type="image" src="../images/save_01_16x16.png" name="id" value="<?php echo $manid; ?>"></td></tr>
            <tr><th>Destino/Unidade</th><td><?php echo $destino ?></td><td><input type="text" name="nomePor" </td><td><input type="image" src="../images/save_01_16x16.png" name="id" value="<?php echo $manid; ?>"></td></tr>
            <tr><th>Setor</th><td><?php echo $setor ?></td><td><input type="text" name="nomePor" </td><td><input type="image" src="../images/save_01_16x16.png" name="id" value="<?php echo $manid; ?>"></td></tr>
            <tr><th>Para/Pessoa</th><td><?php echo $nomepara ?></td><td><input type="text" name="nomePor" </td><td><input type="image" src="../images/save_01_16x16.png" name="id" value="<?php echo $manid; ?>"></td></tr></tr>
            <tr><th>Nome</th><td><?php echo $nome ?></td><td><input type="text" name="nomePor" </td><td><input type="image" src="../images/save_01_16x16.png" name="id" value="<?php echo $manid; ?>"></td></tr></tr>
            <tr><th>Assunto</th><td><?php echo $assunto ?></td><td><input type="text" name="nomePor" </td><td><input type="image" src="../images/save_01_16x16.png" name="id" value="<?php echo $manid; ?>"></td></tr></tr>
            <input type="hidden" name="idd" value="<?php echo $idget; ?>"/> <? // para redirect()?>
        </form>

this is the form that comes out with a layout like this:

    
asked by anonymous 01.07.2016 / 20:07

1 answer

2

To identify which elements of an array are empty, one way is to combine the functions array_keys() , array_filter() , array_flipper() , and array_diff() .

array_keys() gets the keys (name, email, etc) from the 'data array' to inform the user which fields were left blank later.

array_filter() checks if each element of the past array is empty if yes it is not added to the new array.

array_diff() makes the difference of the array of keys that will always be filled with the data array ( $arr ) ie if any element in $invalidos means that some fields came in blank if it means that $arr and $keys are equal and an array with zero elements will be generated, count() does this check.

$arr = ['nome' => '', 'idade' => 0, 'email' => ''];
$keys = array_keys($arr);
$arr = array_filter($arr);
$invalidos = array_diff($keys, array_flip($arr));

if(count($invalidos) > 0){
    'Campos em branco: '. implode(', ', $invalido);
}else{
    echo 'Tudo certo';
}
    
01.07.2016 / 20:36