php - 通知 : Undefined index: image - unable to find the error

标签 php mysql sql html xampp

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

Notice: Undefined index: image in C:\xampp\htdocs\Final\places\Ressave.php on line 27

这是我传递输入名称的 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);
?>

最佳答案

如果文件未上传,$_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">

关于php - 通知 : Undefined index: image - unable to find the error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18747542/

相关文章:

php - 使用 PHP 的 Ajax 时间警报

sql - 使连接中的 "close to"与 "equal"相同

SQL Server 链接数据库别名

mysql - 服务器场如何处理数据库?

php - 2 台云服务器,一名开发人员,一名生产人员;什么是好的部署流程?

php - 如何在redux框架选项中显示上传或默认 Logo

php - ORM 是全有还是全无?

mysql - 如何获取在 MySQL 中没有连接匹配的查询结果?

即使有索引,MySQL 语句也非常慢

php - 如何根据当前时间编写选择查询?