尝试在 PHP 中使用 $_FILE 时出现“未定义索引”错误[关闭]

2023-11-30

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

产品更新.php这是我能够修改数据库中特定记录的所有现有信息的地方。

<?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该文件采用表格的所有值并更新数据库。其他字段已更新,但文件上传不起作用。它给出了错误。

<?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"属性能够上传文件。

更多信息here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试在 PHP 中使用 $_FILE 时出现“未定义索引”错误[关闭] 的相关文章

随机推荐

  • Gradle CreateDebugCoverageReport 不运行本地单元测试

    我正在使用 jacoco 进行代码覆盖 它运行 gradlew createDebugCoverageReport 很好并生成所有的代码覆盖率androidTest案例 但是 它不运行本地单元测试 因此不计算这些测试的代码覆盖率 我怎样才能
  • 在输出中打印 Python 版本

    如何从脚本中打印当前 Python 安装的版本号 Try import sys print sys version 这将打印完整的版本信息字符串 如果你只想要 python 版本号 那么巴斯蒂安 莱昂纳德的解决方案是最好的 您可能想要检查整
  • 正确安装 Android SDK、ADT 和 Eclipse 3.6.1

    按照 Eclipse 3 6 1 Classic 和 Android SDK 发布的说明 ADT 其中 OS 2 3 是最新的 错过了几个步骤 在 Eclipse 中加载 ADT 之前 一切似乎都正常 在此之前 将以下依赖项站点添加到列表中
  • 如何在 Metro/WinRT 中将主机名解析为 IP 地址?

    我正在将 WP7 应用程序移植到 Windows 8 Metro 我遇到的 许多 转换障碍之一是根据主机名或 DNS 名称发现 IP 地址 以下是我之前在WP7中使用的示例 DnsEndPoint dnsEnd new DnsEndPoin
  • xcode sqlite3 libsqlite.dylib

    我的项目出现了错误 但我不知道问题出在哪里 这是我编译后的错误 ld警告 在 install Platforms iPhoneSimulator platform Developer SDKs iPhoneSimulator3 0 sdk
  • 为什么我的 requirejs 配置中的 shim 的 `unit` 没有被调用?

    Update 我正在编写一个小模块来处理这个 csrf 令牌问题backbone直到我收到 Louis 答案的推送通知 他的回答很优雅 看起来不错 但我会离开a link to my 主干 csrfmodule github repo 仅供
  • Homebrew:安装新公式 php72-imagick

    我需要在我的 php 7 2 上安装 imagick 模块 I see brew search php72 gt Searching local taps gt Searching taps on GitHub gt Searching b
  • 我们可以在类中声明密封方法吗

    class X sealed protected virtual void F Console WriteLine X F sealed void F1 protected virtual void F2 Console WriteLine
  • 使用 C++ 将托管事件公开给 COM

    可以公开用 C 编写的托管事件 以便在使用 C 编写的 COM 对象中公开和使用 对 com 和 atl 不太熟悉 您能否展示一下 MSDN 文章中所示示例的 C 方面是什么样子的 http msdn microsoft com en us
  • 使用 ADO 处理 MS Access 中附件类型的字段

    我的数据库中有一些字段需要存储图像 位图 JPG 或 PNG 和 PDF 或 Excel RTF TXT 文件 通过互联网浏览 我了解到MS Access 2007 和更新版本 有一个类型字段Attachment这可以满足我的需要 但是 我
  • 如何获得算术中忘记转换的警告?

    考虑这种情况 uint64 t add uint32 t a uint32 t b return a b programmer neglected uint64 t a b 我们如何让 GCC 或任何其他编译器 的 C 或 C 前端对这种情
  • 使用 iOS/Obj-c 按流派搜索 Apple App store

    您将如何使用 Obj c 搜索 Apple App store 来执行以下操作 返回游戏总体类别中前 100 名的详细信息或 特定游戏类别 返回游戏类别中特定游戏的详细信息 Anyone 嗯 据我所知 您有两种方法来搜索 App Store
  • WPF 中图像填充按钮上的空间

    I have Button在我的 WPF 应用程序上 我想要一个Image来填补Button完全地 目前我有以下代码 它没有填充Button
  • 在 Unix 中根据模式重命名多个文件

    一个目录下有多个以 prefix 开头的文件fgh 例如 fghfilea fghfileb fghfilec 我想将它们全部重命名为以前缀开头jkl 是否有一个命令可以做到这一点 而不是单独重命名每个文件 有多种方法 但使用rename可
  • $().each 与 $.each 与 jQuery 中的 for 循环?

    我不明白为什么会发生这种情况 I read here那 首先 each构成一个single函数调用来启动 迭代器 第二 foo vals each makes three函数调用来启动 迭代器 第一个是 它生成一个新的 jQuery 包装器
  • 使用SQL Server CE;仅当不存在时才可以插入吗?

    我正在尝试验证一个简单的 1 字段表 以确定在插入重复项之前是否存在记录 if not exists select from url where url insert into url 有人可以帮忙吗 您的代码示例将在完整版本的 SQL 中
  • Rails 5.1.2 引导程序图标未在生产中提供

    这是一个很棒的地方 我希望尽快有能力贡献解决方案 拜托 有人帮忙吗 我看了几个小时的 YouTube 并阅读了网络上有关资产管道预编译的每一篇文章 无论出于何种原因 我决定使用 Rails 5 1 2 但我不知道这是否是问题所在 字形图标不
  • “分配”是 @property 编译器指令的默认设置吗?

    如果我定义一个属性并执行以下操作 property nonatomic UIButton button 那么我认为这是一个 分配 属性 那是对的吗 属性参数的默认值为 分配 读写和 原子 没有关键字 原子 只有非原子
  • Session.getActiveUser().getEmail() 不返回任何值

    这是我的第一个 Stackoverflow 问题 所以我尽力在这里做到清晰和结构化 但请原谅我的初学者错误 所以我对 Google 的脚本编辑器有一个小问题 基本上我是在调用Session getActiveUser getEmail 以及
  • 尝试在 PHP 中使用 $_FILE 时出现“未定义索引”错误[关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 我可以轻松地将文件上传到数据库中 但是当我想编辑文件时