vars 中的未定义索引

Undefined Index in vars

我在第 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> 

发生这个问题是因为在第一次加载时 $_POST['nome'] 和 $_POST['preco'] 是空的并且这些索引不存在。

在这种情况下,您应该检查 !empty 到 运行 这行:

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

此时,如果您 post 您的表格与否,这些代码将 运行 这是显示这些通知的根本原因