2016-05-18 103 views
0

我在第26行和第27行的變量中發現了這個錯誤。我一直在尋找這個問題本身,有些人說變量沒有初始化。儘管我認爲他們是。我還看到有人說要使用isset()/!empty(),但我不明白這一點,以及它做了什麼。未定義的變量指數

<?php 

$nome = $_POST['nome']; //26 
$preco = $_POST['preco']; //27 

if(count($_FILES) > 0) { 
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) { 
mysql_connect("localhost", "crc", "root"); 
mysql_select_db ("crc"); 
$imgData =addslashes(file_get_contents($_FILES['userImage'['tmp_name'])); 
$sql = "INSERT INTO fios (nome,preco,imagem)VALUES('$nome','$preco','{$imgData}')"; 
$current_id = mysql_query($sql) or die("<b>Erro:</b> Problema na imagem inserida!<br/>" . mysql_error()); 
if(isset($current_id)) { 
header("Location: veradmin.php"); 
}}} 
?> 

<!DOCTYPE html> 
<html> 
<title>Inserir</title> 
</head> 
<body> 

<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload"> 

<div align="center">   

      </p><tr> 
       <td width="321"><strong>Nome/Descricao:</strong></td> 
       <td width="102" align="left"> 
       <input type="text" name="nome" value="" size="40" /> 

       </td> 
      </tr><p> 

      </p><tr> 
       <td width="321"><strong>Preco:</strong></td> 
       <td width="102" align="left"> 
       <input type="text" maxlength="9" name="preco" value="" size="20" /> 

       </td><p> 
      </p></tr> 

      <input name="userImage" type="file" class="inputFile" /><p> 

      </p><input type="submit" value="Inserir Registo" class="btnSubmit" /> 

</form> 
</div> 
</body> 
</html> 
+4

[PHP的:「注意:未定義的變量」和「注意:未定義的索引」]的可能重複(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Henders

+1

基本上,你不知道'$ _POST ['nome']'實際上包含任何東西。你需要首先用['isset()'](http://php.net/manual/en/function.isset.php)檢查它。 – Henders

+0

替換$ nome = $ _POST ['nome']; // 26 $ preco = $ _POST ['preco']; // 27 by $ nome = $ preco ='';。然後再試一次 –

回答

1

此問題的發生,因爲在第一個載荷$ _ POST [「諾姆」]和$ _ POST [「PRECO」]是空的,這些指標並不存在。 !

在這種情況下,你應該檢查空運行此行:如果您發佈的形式或沒有這些代碼將運行該圖是這些通知的根源

if(!empty($_POST['nome']) && !empty($_POST['preco'])) 
{ 
     if(count($_FILES) > 0) { 
      ... 
     } 
} 

這時