【问题标题】:Undefined Index in vars变量中的未定义索引
【发布时间】:2016-09-14 21:05:20
【问题描述】:

我在第 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'] 实际上包含任何内容。您需要先使用isset() 进行检查
  • 替换 $nome = $_POST['nome']; //26 $preco = $_POST['preco']; //27 由 $nome = $preco = '';.然后再试一次
  • 第 26 行和第 27 行 _ 是什么??
  • 我已经看到了,它没有做任何更改,仍然有同样的错误。

标签: php mysql var


【解决方案1】:

出现此问题是因为在第一次加载时 $_POST['nome'] 和 $_POST['preco'] 为空且这些索引不存在。

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

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

此时,无论您是否发布表单,这些代码都会运行,这是显示这些通知的根本原因

【讨论】:

    猜你喜欢
    • 2015-01-20
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 2019-02-23
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多