【问题标题】:Notice: Undefined index: image - unable to find the error注意:未定义索引:图像 - 无法找到错误
【发布时间】:2013-09-15 20:27:52
【问题描述】:

这是我的错误。我不知道为什么它不起作用。我检查了所有部分,但我找不到错误。

注意:未定义索引:第 27 行 C:\xampp\htdocs\Final\places\Ressave.php 中的图像

这是我传递输入名称的 html 代码:

<form class="form-horizontal" action="Ressave.php" method="POST" autocomplete="on">
<div class="well">
   <legend>Photos</legend>
      <div class="control-group">
         <label class="control-label">Upload Photo: </label>
             <div class="controls">
                   <input name="image" type="file" />
             </div>   
     </div>
</div>

            <div class="form-actions">

              <button type="submit" class="btn btn-primary">Submit</button>

              <button type="button" class="btn">Cancel</button>

            </div>

        </form>

Ressave.php 在这里,无法在此处接收名称,所以出现错误.....

<?php
{       //  Secure Connection Script
    include('../Secure/dbConfig.php'); 
    $dbSuccess = false;
    $dbConnected = mysql_connect($db['hostname'],$db['username'],$db['password']);

    if ($dbConnected) {     
        $dbSelected = mysql_select_db($db['database'],$dbConnected);
        if ($dbSelected) {
            $dbSuccess = true;
        } else {
            echo "DB Selection FAILed";
        }
    } else {
            echo "MySQL Connection FAILed";
    }
    //  END Secure Connection Script
}
if(! $dbConnected )
{
  die('Could not connect: ' . mysql_error());
}


{               // File Properties

$file = $_FILES['image']['tmp_name'];   //Error comes from here(here is the prob!)


if(!isset($file))

    echo "Please Choose an Image.";

else  {
        $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));

        $image_size = getimagesize($_FILES['image']['tmp_name']);   

        if($image_size == FALSE)
            echo "That is not an image.";
        else 
        {
                $lastid = mysql_insert_id();
                echo "Image Uploaded.";             
        }
}

}

{           //join the post values into comma separated 

    $features = mysql_real_escape_string(implode(',', $_POST['features']));
    $parking = mysql_real_escape_string(implode(',', $_POST['parking']));   
    $noise = mysql_real_escape_string(implode(',', $_POST['noise']));       
    $good_for = mysql_real_escape_string(implode(',', $_POST['good_for']));
    $ambience = mysql_real_escape_string(implode(',', $_POST['ambience']));
    $alcohol = mysql_real_escape_string(implode(',', $_POST['alcohol']));


}


$sql =  "INSERT INTO prestaurant ( ResName, Rating, Food_serve, Features, Parking, noise, Good_For, Ambience, Alcohol, Addition_info, Name, Address1, Zipcode1, Address2, Zipcode2, Address3, Zipcode3, City, Mobile, phone1, phone2, phone3, phone4, Email1, Email2, Fax, Website, image)". 
        "VALUES ('$_POST[restaurant_name]','$_POST[star_rating]','$_POST[food_served]','$features','$parking','$noise','$good_for','$ambience','$alcohol','$_POST[description]','$_POST[name]','$_POST[address1]','$_POST[zipcode1]','$_POST[address2]','$_POST[zipcode2]','$_POST[address3]','$_POST[zipcode3]','$_POST[city]','$_POST[mobile]','$_POST[phone1]','$_POST[phone2]','$_POST[phone3]','$_POST[phone4]','$_POST[email1]','$_POST[email2]','$_POST[fax]','$_POST[url]','$image')";


mysql_select_db('place');
$retval = mysql_query( $sql );
if(! $retval )
{    
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($dbConnected);
?>

【问题讨论】:

  • 即使您选择了要上传的有效图像文件,您是否也会收到错误消息?
  • 我认为您的代码 ( {} ) 中可能有一些额外的和多余的花括号,您可以尝试清理您的代码,看看它是否有任何改变?此外,我没有看到任何可以将文件实际上传到您的服务器的代码。
  • 你知道相同的代码如何在其他地方工作,但是当我在这里使用它时,我不知道为什么会出错
  • @EniGma 我在if(!isset($file)) echo "Please Choose an Image."; 中注意到没有左大括号或右大括号。这可能是这里的一个因素。应该读为if(!isset($file)) { echo "Please Choose an Image."; }

标签: php mysql sql html xampp


【解决方案1】:

如果没有上传文件,$_FILES 数组将为空。具体来说,如果image文件没有上传,$_FILES['image']就不会被设置。

所以

$file = $_FILES['image']['tmp_name'];   //Error comes from here(here is the prob!)

应该是:

if(empty($_FILES) || !isset($_FILES['image']))

更新

您还会遇到问题,因为您缺少表单上的 enctype 属性:

<form class="form-horizontal" action="Ressave.php" method="POST" autocomplete="on" enctype="multipart/form-data">

【讨论】:

  • 错误是数组 ( ) 请选择一个图像。注意:未定义变量:第 64 行 C:\xampp\htdocs\Final\places\Ressave.php 中的图像
  • 我的相同代码可以在其他地方工作,但在我的这部分代码中不起作用
  • @JohnConde 我开始怀疑 OP 是否想要对上传文件的简单引用,或者 OP 是否想要将文件上传到实际文件夹。 (使用move_uploaded_file),这不在OP的代码中。
  • 当我使用 print_r($_POST['image']);它将显示图像的名称,例如(winter.jpg)
  • 我使用 john 的代码 (if(empty($_FILES) || !isset($_FILES['image']))) 并且我成功了...
【解决方案2】:

为了能够处理表单中的文件,您需要添加 enctype 属性。

<form method='POST' enctype='multipart/form-data' >

【讨论】:

【解决方案3】:

嘿,我猜你忘记了表单中的一个重要设置 enctype="multipart/form-data" 此选项用于文件,例如图像文件等

<form name="image" method="post" enctype="multipart/form-data">

除此之外,您还可以使用以下选项来提取文件的内容

$tmp_img_path = $_FILES['image']['tmp_name'];
$img_name = $_FILES['image']['name'];

打印文件的所有内容:

print_r($_POST['image']);

【讨论】:

    【解决方案4】:

    这是我成功将数据插入 mysql 并从数据库中检索的代码。

    “索引.PHP”

    <html>
    <head>
    <title>PHP & MySQL: Upload an image</title>
    </head>
    <body>
    <form action="index.php" method="POST" enctype="multipart/form-data">
    File: <input type="file" name="image" /><input type="submit" value="Upload" />
    </form>
    
    <?php
    mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("registrations") or die(mysql_error());
    if(!isset($_FILES['image']))
    {
    echo 'Please select an image.';
    }
    else {
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    echo $_FILES['image']['tmp_name'];
    $image_name = addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);
    
    if($image_size==FALSE){
    echo "That's not an image.";
    } else {
    if(!$insert = mysql_query("INSERT INTO test_image VALUES ('','$image_name','$image')"))
         {
     echo "Problem uploading image.";
     } else {
      $lastid = mysql_insert_id();
      echo "Image uploaded.<p />Your image:<p /><img src=get.php?id=$lastid>";
      }
    }
    
      }
    ?>
    </body>
    </html>
    

    这是get.PHP

    <?php
    mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("registrations") or die(mysql_error());
    $id = addslashes($_REQUEST['id']);
    $image = mysql_query("SELECT * FROM test_image WHERE id=$id");
    $image = mysql_fetch_assoc($image);
    $image = $image['image'];
    header("Content-type: image/jpeg");
    echo $image;
    ?>
    

    注意:- 请相应地更改您的数据库和表名.. 谢谢, 桑塔努

    【讨论】:

      猜你喜欢
      • 2014-06-25
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 2012-10-18
      • 1970-01-01
      • 2020-09-28
      • 2015-12-01
      相关资源
      最近更新 更多