尝试在PHP中使用$ _FILE时出现“未定义索引"错误 [英] Getting 'undefined index' error while trying to use $_FILE in PHP

查看:105
本文介绍了尝试在PHP中使用$ _FILE时出现“未定义索引"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以轻松地将文件上传到数据库中,但是当我要编辑文件时,出现未定义"错误.这是代码:

I can easily upload the files into the database But when i want to edit the files,I am getting Undefined error. Here is the code:

product_update.php ,在这里我可以修改数据库中特定记录的所有现有信息.

product_update.php This is where i am able to modify all the existing information of particular records in the database.

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="purchase_try"; // Database name 
$tbl_name="product_list"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");


// get value of id that sent from address bar
//$id = $_GET['AutoID'];

if(isset($_GET['AutoID'])){
$id= $_GET['AutoID'];
}
// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE AutoID = ".$id;

$result=mysql_query($sql);

$rows=mysql_fetch_array($result, 1);

?>


            <form name="form1" method="post" action="update_post.php"/>
            <table width="600" border="0" cellspacing="1" cellpadding="0">

            <tr>
            <td>
            <table width="100%" height="87" border="1" cellpadding="3" cellspacing="0">
            <tr>
            <td colspan="3"><table width="100%" height="87" border="1" cellpadding="3" cellspacing="0">

            <tr>
            <td align="center"><strong>Title</strong></td>
            <td width="144" align="center"> : </td>
            <td > <input name="title_product" type="text" id="title_product" value="<?php echo $rows['title_product']; ?>" size="90" /></td>
            </tr>

            <tr>
            <td align="center"><p><strong>Description</strong></p></td>
            <td align="center"> : </td>
            <td><input name="description_product" type="text" id="description_product" value="<?php echo $rows['description_product']; ?>" />
            </td>
            </tr>

            <tr>
            <td width="169" align="center"><p><strong>Start Date</strong></p></td>
            <td align="center"> : </td>
            <td><input name="start_date" type="text" id="start_date" value="<?php echo $rows['start_date']; ?>" size="40" /> </td>
            </tr>

            <tr>
            <td width="169" align="center"><p><strong>End Date</strong></p></td>
            <td align="center"> : </td>
            <td><input name="end_date" type="text" id="end_date" value="<?php echo $rows['end_date']; ?>" size="40" /></td>
            </tr>

            <tr>
            <td width="169" align="center"><strong>Price Before</strong></td>
            <td align="center"> : </td>
            <td> <input name="price_before" type="text" id="price_before" value="<?php echo $rows['price_before']; ?>" size="40" /> </td>
            </tr>

            <tr>
            <td width="169" align="center"><strong>Price After </strong></td>
            <td align="center"> : </td>
            <td> <input name="price_after" type="text" id="price_after" value="<?php echo $rows['price_after']; ?>" size="40" /> </td>
            </tr>

            <tr>
            <td width="169" align="center"><strong>Percentage </strong></td>
            <td align="center"> : </td>
            <td><input name="percentage" type="text" id="percentage" value="<?php echo $rows['percentage']; ?>" size="40" /></td>
            </tr>

            <tr>
            <td width="169" align="center"><strong>Edit Uploaded Images?<!-- <a href="image_edit.php?AutoID=<?PHP //echo $rows['AutoID'];?>">Click here.</a> --></strong></td>
          < <td align="center"> : </td>
            <td><input name="ufile1" type="file" id="ufile1" size="50" /></td>
            </tr>

            <tr>
          <td width="169" align="center"><strong>Image 2 </strong></td>
            <td align="center"> : </td>
            <td><input name="ufile2" type="file" id="ufile2" size="50" /></td>
            </tr>

            <tr>
            <td width="169" align="center"><strong>Image 3 </strong></td>
            <td align="center"> : </td>
            <td><input name="ufile3" type="file" id="ufile3" size="50" /></td> 
            </tr>

            <tr>
            <td width="169" align="center"><strong>Date Created</strong></td>
            <td align="center"> : </td>
            <td><input name="date_created" type="text" id="date_created" value="<?php echo $rows['date_created']; ?>" /> </td>
            </tr>

            <tr>
            <td width="169" align="center"><strong>Time Created</strong></td>
            <td align="center"> : </td>
            <td><input name="time_created" type="text" id="time_created" value="<?php echo $rows['time_created']; ?>" /></td>
            </tr>

        <td colspan="3"><p align="right">
        <input name="AutoID" type="hidden" id="AutoID" value="<?php echo $rows['AutoID']; ?>" />
        <input type="submit" name="Submit" value="Submit" />
        </p></td>
        </tr>
        </table></td>
</tr>
</table>
</td>

</tr>
</td>
</form>

<?php

// close connection 
mysql_close();

?>

然后,Update_Post.php .此文件获取FORM的所有值并更新数据库.其他字段已更新,但FILE UPLOAD不起作用.它给出了错误.

then, Update_Post.php This File takes all the Values of the FORM and UPDATES the database. Other fields are updated BUT the FILE UPLOAD doesn't work. It gives error.

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="purchase_try"; // Database name 
$tbl_name="product_list"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database

?> 
<?PHP
 if(isset($_POST['Submit'])) 
    {



    $title          = $_POST['title_product'];
    $description    = $_POST['description_product'];

    $start_date     = $_POST['start_date'];
    $end_date       = $_POST['end_date'];
    $price_before   = $_POST['price_before'];
    $price_after    = $_POST['price_after'];
    $percentage     = $_POST['percentage'];

    $image_1        = $_FILES['ufile1']['name'];
    $image_2        = $_FILES['ufile2']['name'];
    $image_3        = $_FILES['ufile3']['name'];

    $date_created   = $_POST['date_created'];
    $time_created   = $_POST['time_created'];

    $id                 = $_POST['AutoID'];

    $sql="UPDATE $tbl_name SET title_product='".$title."',
                                    description_product='".$description."',
                                    start_date='".$start_date."',
                                    end_date ='".$end_date."',
                                    price_before='".$price_before."',
                                    price_after='".$price_after."',
                                    percentage='".$percentage."',

                                    date_created='".$date_created."',
                                    time_created='".$time_created."' WHERE AutoID=".$id; 

    $result=mysql_query($sql);
//  image_1 = '$image_1',
    //                              image_2 = '$image_2',
    //                              image_3 = '$image_3',

    //$sql_insert = "INSERT INTO $tbl_name(image_1, image_2, image_3)VALUES ('$image_1', 'image_2', 'image_3') WHERE AutoID= ".$id;

    //$sql_result = mysql_query($sql_insert);
    // if successfully updated. 
    if($result){

                    $path1= "upload/".$_FILES['ufile1']['name'];
                    $path2= "upload/".$_FILES['ufile2']['name'];
                    $path3= "upload/".$_FILES['ufile3']['name'];

                    //copy file to where you want to store file
                    copy($_FILES['ufile1']['tmp_name'], $path1);
                    copy($_FILES['ufile2']['tmp_name'], $path2);
                    copy($_FILES['ufile3']['tmp_name'], $path3);
                    //$_FILES['ufile']['name'] = file name
                    //$_FILES['ufile']['size'] = file size
                    //$_FILES['ufile']['type'] = type of file

                    // Use this code to display the error or success.

                    $filesize1=$_FILES['ufile1']['size'];
                    $filesize2=$_FILES['ufile2']['size'];
                    $filesize3=$_FILES['ufile3']['size'];

                    //all 3 must be selected to upload.
                    if($filesize1 || $filesize2 || $filesize3 != 0) 
                    {
                    $sql_insert = "INSERT INTO $tbl_name(image_1, image_2, image_3)VALUES ('$image_1', 'image_2', 'image_3') WHERE AutoID= ".$id;

                    $sql_result = mysql_query($sql_insert);
                    echo "We have recieved your files";
                    }

                    else {
                    echo "ERROR.....";
                    }
                    echo "Successful";

                    //header("location:user_history2.php");
                    } 
    }

    else 
    {
    echo "Update Unsuccessful.";
    }

?>

The is Undefined index : ufile

推荐答案

您的表单需要enctype="multipart/form-data"属性才能上传文件.

Your form needs the enctype="multipart/form-data" attribute to be able to upload files.

更多信息此处

这篇关于尝试在PHP中使用$ _FILE时出现“未定义索引"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆