I am making a php form and I need to update the DB if a user makes changes to the data

Asked

Viewed 45 times

0

I know how to do this by if Else, but I have 8 fields where you can only have 1,2,3... blank fields and I have to check if the field is empty and if you are not updating in DB, I wanted to know if there is easier 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: inserir a descrição da imagem aqui

  • Gives an example of code in the question, is an array or variables 'loose'?

  • I’ve set examples, but I’ve come up with a solution to my problem, thank you.

  • I already solved the problem using the default html resource.

  • Used the require ?

  • Did any of the answers solve your problem? In that case you can mark as accepted. Or you can give me an answer like you did but explain how you solved the problem. Just as this is too short to be an answer and won’t help others who might have the same problem.

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() obtains the keys(name, email etc) of the data array to inform the user which fields were left blank later.

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

array_diff() makes the difference of the key array that will always be filled with the data array($arr) that is if any element in $invalidos means that some fields came blank if counting means that $arr and $keys are equal and an array with zero elements will be generated, the count() makes that 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';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.