Undefined variable when updating the Database

-1

I have a problem doing Database Update.

$AcidenteValidade = $_POST["AcidenteValidade"]; 

$AcidenteAnexo='';
if (isset($_FILES["AcidenteAnexo"]) && $_FILES["AcidenteAnexo"]["name"] != '') {

    $nomeTemporario = $_FILES["AcidenteAnexo"]["tmp_name"]; 

    $fp = fopen($nomeTemporario, 'r'); 
    $AcidenteAnexo = fread($fp, filesize($nomeTemporario)); 
    $AcidenteAnexo = addslashes($AcidenteAnexo);

    fclose($fp); 
}
$consulta2 = "UPDATE tb_trabalhador SET AcidenteAnexo='.$AcidenteAnexo.' WHERE id=$id";
mysql_query($consulta2) or die(mysql_error());

The problem is AcidenteAnexo that appears as:

  

Undefined. Notice: Undefined variable: AccidentAnexo

    
asked by anonymous 01.04.2014 / 12:50

3 answers

1

Thanks, guys. I skipped a field when I received the data.

enctype="multipart/form-data"

$AcidenteAnexo='';
if (isset($_FILES["AcidenteAnexo"]) && $_FILES["AcidenteAnexo"]["name"] != '') {

$nomeTemporario = $_FILES["AcidenteAnexo"]["tmp_name"]; 

$fp = fopen($nomeTemporario, 'r'); 
$AcidenteAnexo = fread($fp, filesize($nomeTemporario)); 
$AcidenteAnexo = addslashes($AcidenteAnexo);

fclose($fp); }
$consulta2 = "UPDATE tb_trabalhador SET AcidenteAnexo='.$AcidenteAnexo.' WHERE id=$id";
mysql_query($consulta2) or die(mysql_error());
    
01.04.2014 / 13:21
1

The variable $AcidenteAnexo is undefined because its if is returning false, as it only begins to exist inside it, so if the condition of if is not satisfied $AcidenteAnexo will not exist.

if (isset($_FILES["AcidenteAnexo"]) && $_FILES["AcidenteAnexo"]["name"] != '') {
    $AcidenteAnexo = addslashes($AcidenteAnexo);
}

$consulta2 = "UPDATE tb_trabalhador SET AcidenteAnexo='$AcidenteAnexo' WHERE id=$id";
mysql_query($consulta2) or die(mysql_error());

The solution would put the update inside if, since the update will run whenever $_FILES has value.

The error can be reproduced in a simplified way with this code

if(false){
    $nome = 'mario';
}
echo $nome;

output:

    Notice: Undefined variable: nome in
    
01.04.2014 / 12:58
0

You are not using the value of your variable in your update, you have to change to read the value of the variable.

You're like this:

$consulta2 = "UPDATE tb_trabalhador SET AcidenteAnexo='$AcidenteAnexo' WHERE id=$id";

The right thing would be:

$consulta2 = 'UPDATE tb_trabalhador SET AcidenteAnexo=' . $AcidenteAnexo . ' WHERE id=' . $id;

Well, you concatenate the texts of your update with the value of $AcidenteAnexo and $id .

    
01.04.2014 / 13:00