2016-05-18 105 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) { 
      ... 
     } 
} 

这时