Undefined variable when updating Database

Asked

Viewed 210 times

-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 the AcidenteAnexo that appears as:

Indefinite. Notice: Undefined variable: Accidentalannexe

  • in which line you’re making the mistake?

  • what appears if you do this: echo $nameTemporario = $_FILES["Accidental"]["tmp_name"]; ???

  • The error line is the line Query 2

  • 1

    so you never get into if, right? guess you can’t just use $_FILES["Accidentalannex"].

  • If is for me to check whether or not there is a modified document

  • Reviewing this, I edited "update of Database" and maybe it would be better to "update in Database". . This is the suggestion for the next editor.

Show 1 more comment

3 answers

1

The variable $AcidenteAnexo is undefined because your if is returning false, as it only begins to exist within it, so if the if condition is not satisfied $AcidenteAnexo there will be no.

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());

To solution would put the update inside if, since the update will be executed whenever $_FILES have value.

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

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

exit:

    Notice: Undefined variable: nome in
  • I changed it just like this in the question.

1

Thank you guys. I’d jump a field when I got 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());
  • 2

    Which field? could better describe how you solved the problem? its explanation can help several people with the same problem.

0

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

It’s 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;

So you concatenate the texts of your update with the variable value $AcidenteAnexo and of $id.

Browser other questions tagged

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