$ path =" Image /"。$ _ FILES [" image"] [" name"];

时间:2014-06-12 09:29:09

标签: php

  

"未定义的索引:图像在   第34行和第34行的C:\ xampp \ htdocs \ learn_php \ Category \ 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 个答案:

答案 0 :(得分:0)

在您的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">

答案 1 :(得分:0)

在您的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']第1行条件中的insertup.php可能为空,请将其更改为

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