2
I want to know how the error handling should be done, correctly, for a non-compulsory variable, since if it is not informed, a Fatal Error - Undefined Index
.
Context:
Through my system I register a publication (documents for the user to view), and create a folder system for the user to access the data later.
Problem:
When a publisher will register the publication, he can inform all fields or some (in case my question is specifically, about the fields fk_empregado
and mes
), that when there is a data register and is not informed fk_empregado
or the field mes
it generates the following Notice
:
Notice: Undefined index: fk_empregado in ... on line 14 //Linha que recebe através de POST os dados do campo empregado
Notice: Undefined index: mes in ... on line 19 //Linha que recebe através de POST os dados do campo mes
So here’s my question, exactly as I can inform the script that there is no problem these two fields are undefined, and that it can continue normally(I don’t know the answer, but I’m sure it’s not with the @
)?
Code of the Insert Page :
$fk_titulo = $_POST['publicacao'];
$fk_tipo = $_POST['tipo_publicacao'];
$fk_empregado = $_POST['empregado'];
$fk_empresa = $_POST['empresa_destino'];
$data_vencimento = $_POST['data_vencimento'];
$data_pagamento = $_POST['data_pagamento'];
$arqName = $_FILES['arquivo']['name'];
$mes = $_POST['mes'];
$ano = $_POST['ano'];
$valor = $_POST['valor'];
$publicante = $_SESSION['nome'];
$observacao = $_POST['observacao'];
$status = "N";
$dir = 'upload/publicacoes/' . implode('/', array_filter([$fk_empresa, $fk_tipo, $fk_titulo, $fk_empregado, $ano, $mes]))."/";
You can send them without having any value, so I think there is no error (I do not know very well the behavior of PHP)
– Artur Trapp
When they are received empty a Notice Undefined Index is generated
– UzumakiArtanis