$ path =“Image /".$_ FILES [”image“] [”name“];

问题描述:

“未定义指数:图像中 C:\ XAMPP \ htdocs中\ learn_php \在线路34上分类\ insertup.php” 这个错误 被示出,并且线34是 -

$path= "Image/".$_FILES["image"]["name"]; 

EDIT.PHP文件: -

<?php 
if(isset($_GET['id']) && $_GET['id'] != '') { 
mysql_connect('localhost', 'root', ''); 
mysql_select_db('opencart_php'); 
    $id=$_GET['id']; 

$query=mysql_query("SELECT * FROM categories WHERE Id='".$id."'"); 
    $row=mysql_fetch_array($query); 
    $cname = $row['category_name']; 
    $sname = $row['sort_order']; 
    $desc = $row['description']; 
    $image = $row['image']; 
} else { 
    $id = ''; 
    $cname = ''; 
    $sname = ''; 
    $desc = ''; 
    $image = ''; 
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Category Details</title>` 
<h1 align="center">Category Details</h1> 
</head> 

<body> 
<table width="400" border="1" align="center"> 
<tr> 
<td><form name="myform" action="insertup.php?id=<?php echo $id; ?>" method="post" enctype="multipart/form_data"> 
<table width="334" border="0" align="center"> 
    <tr> 
    <td><b>Category Name:</b></td> 
    <td><input type="text" name="cname" value="<?php echo $cname; ?>"></td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr> 
    <td><b>Sort Order:</b></td> 
    <td><input type="text" name="sorder" size="5" value="<?php echo $sname; ?>"></td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr> 
    <td height="40"><b>Description:</b></td> 
    <td><input type="text" rows="10" cols="20" name="desc" value="<?php echo $desc; ?>" /></td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr> 
    <td height="37"><b>Image:</b></td> 
    <td><input type="file" name="image" id="image"></td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr> 
    <td><b>Status:</b></td> 
    <td><select name="status"> 
      <option value='1'>Enable</option> 
      <option value='0'>Disable</option> 
     </select></td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
</table> 
<table width="368"> 
<tr> 
<td width="173" align="right"><input type="submit" name="submit" value="Submit"></td> 
<td width="183" align="center"><input type="reset" name="reset" value="Reset"></td> 
</tr> 
</table> 
</form></td> 
</tr> 
</table> 
</body> 
</html> 

INSERTUP.PHP: -

if($_POST["cname"] != "" && $_POST["desc"] != "" && $_POST["image"] != "") { 
     $_SESSION['cname']=$_POST["cname"]; 
     //echo $_SESSION['cname']; 
     $query=mysql_query("SELECT * FROM categories WHERE category_name='".$_SESSION['cname']."'"); 
     $row=mysql_fetch_array($query); 
     $c_name=$row['category_name']; 
     $img=$row['image']; 
     echo $c_name; 
     //$id=$row['id']; 
     //if(isset($_FILES["image"])) { 
     print_r($_FILES); 
      if($cname != $c_name && $image != $img) { 
       $path= "Image/".$_FILES["image"]["name"]; 
       if($path != '') { 
        if(copy($_FILES["image"]["tmp_name"], $path)) { 
         $sql=mysql_query("INSERT INTO categories (category_name, sort_order, description, image, status) 
          VALUES ('$cname', '$sorder', '$desc', '$image', '$status')") or die(mysql_error()); 
         header('location: index.php'); 
        } else { 
        echo "Error in Insertion"; 
        } 
       } else { 
        echo "Image not Uploaded"; 
       } 
      } //} 
    } 
} 
+2

what's的print_r($ _ FILES)的'输出;'? – panther

+0

它显示阵列()........ print_r的 – user3733306

在HTML中有(form-data)

变化

<form name="myform" action="insertup.php?id=<?php echo $id; ?>" method="post" enctype="multipart/form_data"> 

<form name="myform" action="insertup.php?id=<?php echo $id; ?>" method="post" enctype="multipart/form-data"> 
+0

后并未奏效...... – user3733306

+0

什么是不工作?您是否正在做的print_r($后)的$ _FILES – Anish

在你的HTML中的错字,在form必须是enctype="multipart/form-data",不是enctype="multipart/form_data"

<form name="myform" action="insertup.php?id=<?php echo $id; ?>" method="post" enctype="multipart/form-data"> 

然后,$_POST['image']上的insertup.php 1线你的病情很可能是空的,所以它改成

if($_POST["cname"] != "" && $_POST["desc"] != "" && !empty($_FILES["image"]["name"])) { 
+0

值混得insertup.php值不是从edit.php页移动到页面insertup.php ...... plz帮助 – user3733306

+0

如果固定'ENCTYPE 'attr,并且文件输入真的有'name = image',它应该可以工作。 – panther