未定义的索引

时间:2016-01-05 11:35:49

标签: php undefined-index

我在此代码中收到未定义索引:图像错误。我能知道确切的解决方案吗?我想知道从abc到xyz的工作流程,我在代码中使用//评论。谢谢你的帮助..

   <?php session_start();
  include("config.php");
 if(isset($_SESSION['name']))
 {
if(!$_SESSION['name']=='admin')
{
header("Location:login.php?id=You are not authorised to access this page unless you are administrator of this website");
}
}
?>
 <?php
  $name=$_FILES['image']['name'];
 $tmp=$_FILES['image']['tmp_name'];
  $err=$_FILES['image']['error'];
   }
 if($err==0)
 {
 move_uploaded_file($tmp, $name);
  //xyz}
 $category=$_POST['category'];
 $title=$_POST['title'];
$image=$_FILES["image"]["name"];
$content=$_POST['content'];
}
<?php
 $qry=mysql_query("INSERT INTO articles(title,image,content,category)VALUES('$title','$image','$content','$category')");
if(!$qry)
 {
die("Query Failed: ". mysql_error());
}
else
{
echo "Article Added Successfully";
}
 ?>

  The form code is here:
<?php
include("config.php");
$sql=mysql_query("select * from category");
if(!$sql)
{
mysql_error();  
}
?>
<form action="created_article.php" method="post">
Category:
<select name="category">
<?php
while($row=mysql_fetch_array($sql))
{
echo"<option value='".$row['category']."'>".$row['category']."</option>";
}
?>
</select>
Title:
<input type="text" name="title"/>
Upload Image:
<input type="file" name="image" id="image" />
Contents:
<textarea name="content" cols="100" rows="12" ></textarea>
<input type="submit" name="button"  value="Submit" />
</form>

我需要这些代码的帮助,我需要制作项目而且我被困在这里,所以请请求你的帮助,

2 个答案:

答案 0 :(得分:0)

<form action="created_article.php" method="post" enctype="multipart/form-data">

在表单标记中定义enctype,否则它不适用于图像

答案 1 :(得分:0)

在处理输入文件时始终使用表单属性enctype =&#39; multipart / form-data&#39;

<?php

session_start ();
include ("config.php");
if (isset ( $_SESSION ['name'] )) {
    if (! $_SESSION ['name'] == 'admin') {
        header ( "Location:login.php?id=You are not authorised to access this page unless you are administrator of this website" );
    }
}
if (!empty($_POST))
{
    $name = $_FILES ['image'] ['name'];
    $tmp = $_FILES ['image'] ['tmp_name'];
    $err = $_FILES ['image'] ['error'];


    if($err==0) {
        move_uploaded_file($tmp, $name);

        $category=$_POST['category'];
        $title=$_POST['title'];
        $image=$_FILES["image"]["name"];
        $content=$_POST['content'];
    }

    $qry=mysql_query("INSERT INTO articles(title,image,content,category)VALUES('$title','$image','$content','$category')");

    if(!$qry){
        die("Query Failed: ". mysql_error());

    } else {
       echo "Article Added Successfully";

    }

 }
    include("config.php");
    $sql=mysql_query("select * from category");

    if(!$sql){

        mysql_error();  
    }
?>
The form code is here:
<form action="created_article.php" enctype='multipart/form-data' method="post">
    Category: <select name="category">
<?php
while($row=mysql_fetch_array($sql))
{
echo"<option value='".$row['category']."'>".$row['category']."</option>";
}
?>
</select> Title: <input type="text" name="title" /> Upload Image: <input
        type="file" name="image" id="image" /> Contents:
    <textarea name="content" cols="100" rows="12"></textarea>
    <input type="submit" name="button" value="Submit" />
</form>