提问人:Sam Stevenson 提问时间:3/5/2016 最后编辑:Alive to die - AnantSam Stevenson 更新时间:3/5/2016 访问量:1115
使用 $_FILES 上传图片时未定义索引 [重复]
Undefined Index when using $_FILES to upload a picture [duplicate]
问:
我正在使用 创建一个应用程序,我根本不是这方面的专家,但我正在慢慢完成它。PHP
当允许用户在图像旁边上传信息时,我遇到了一个问题。
以下是我设置的表单,允许用户输入所有信息并选择他们希望上传的图像:
<form action="NewOwnedItem.php" method="POST">
<div class="input-row">
<label>Item's Name</label>
<input type="text" name="Name" placeholder="Enter your item's name">
</div>
<div class="input-row">
<label>Image</label>
<input type="file" name="file_img">
</div>
<div class="input-row">
<label>Description</label>
<input type="text" name="Description" placeholder="Enter your item's description">
</div>
<div class="input-row">
<label>Quantity</label>
<input type="number" name="Quantity" min="1" max="99" placeholder="Enter the quantity you own of this item">
</div>
<div class="input-row">
<label>Price</label>
<input type="text" name="Price" placeholder="Enter the price you paid for this item">
</div>
<div class="input-row">
<label>Condition</label>
<select name="Condition">
<option value="Very Poor">Very Poor</option>
<option value="Poor">Poor</option>
<option value="Fair">Fair</option>
<option value="Good">Good</option>
<option value="Very Good">Very Good</option>
<option value="Excellent">Excellent</option>
<option value="Near Mint">Near Mint</option>
<option value="Mint">Mint</option>
</select>
</div>
<div class="input-row">
<label>Date Purchased</label>
<input type="date" name="DatePurchased">
</div>
<input type="submit" value="Add Item" name="submit">
</form>
下面是获取此信息并将其存储在数据库中的 PHP 代码(与数据库的连接在文件中的前面,因此此处未显示)。 我想要它,以便图像 URL 存储在数据库中,而实际图像存储在服务器上的“上传”文件中,我按照该部分代码的教程进行操作,但错误仍然出现。
注意:未定义索引:file_img 英寸 /web/users/n3071633/FYP/NewOwnedItem.php 上线 50 注意:未定义 索引:file_img /web/users/n3071633/FYP/NewOwnedItem.php 在线 51
这些是出现的错误,下面是PHP代码。
<?php
if(isset($_POST['submit'])){
$Name = mysql_escape_string($_POST['Name']);
$Description = mysql_escape_string($_POST['Description']);
$Quantity = mysql_escape_string($_POST['Quantity']);
$Price = mysql_escape_string($_POST['Price']);
$Condition = mysql_escape_string($_POST['Condition']);
$DatePurchased = mysql_escape_string($_POST['DatePurchased']);
$filetmp = $_FILES["file_img"]["tmp_name"];
$filename = $_FILES["file_img"]["name"];
$filepath = "uploads/".$filename;
move_uploaded_file($filetmp,$filepath);
mysql_query("INSERT INTO `CS_Items` (`Item_ID`, `Name`, `Image`, `Description`, `Quantity`, `Price`, `ItemCondition`, `DatePurchased`, `User_ID`, `ItemStatus`) VALUES (Null, '$Name', '$filepath', '$Description', '$Quantity', '$Price', '$Condition', '$DatePurchased', '$login_session', 'Owned') ");
}
?>
让我感到困惑的是,表单的所有其他部分都有效,但是包含的那些似乎是显然没有定义的那些?$_FILES
我试过查看其他问题和答案,但是答案似乎是我的代码中已经存在的东西?任何帮助将不胜感激
答:
1赞
Death-is-the-real-truth
3/5/2016
#1
你忘了添加你的所以,没有你,你永远不会工作。enctype="multipart/form-data"
<form action="NewOwnedItem.php" method="POST">
$_FILES
所以让它像:-
<form action="NewOwnedItem.php" method="POST" enctype="multipart/form-data">
评论
1赞
Sam Stevenson
3/5/2016
谢谢 Anant,它让错误消失了,并在数据库中上传了文件路径!
评论
enctype = "multipart/form-data"
<form action="NewOwnedItem.php" method="POST">
$_FILES