使用$_FILES上传图片时Undefined Index
Undefined Index when using $_FILES to upload a picture
我正在使用 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 存储在数据库中,而实际图像存储在服务器上的 "upload" 文件中,我遵循了该部分代码的教程但是错误仍然出现。
Notice: Undefined index: file_img in
/web/users/n3071633/FYP/NewOwnedItem.php on line 50 Notice: Undefined
index: file_img in /web/users/n3071633/FYP/NewOwnedItem.php on line
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
的部分似乎显然没有定义?
我已经尝试查看其他问题和答案,但是答案似乎已经在我的代码中了?任何帮助将不胜感激
您忘记在 <form action="NewOwnedItem.php" method="POST">
中添加 enctype="multipart/form-data"
,否则您将无法使用 $_FILES
。
所以让它像:-
<form action="NewOwnedItem.php" method="POST" enctype="multipart/form-data">
我正在使用 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 存储在数据库中,而实际图像存储在服务器上的 "upload" 文件中,我遵循了该部分代码的教程但是错误仍然出现。
Notice: Undefined index: file_img in /web/users/n3071633/FYP/NewOwnedItem.php on line 50 Notice: Undefined index: file_img in /web/users/n3071633/FYP/NewOwnedItem.php on line 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
的部分似乎显然没有定义?
我已经尝试查看其他问题和答案,但是答案似乎已经在我的代码中了?任何帮助将不胜感激
您忘记在 <form action="NewOwnedItem.php" method="POST">
中添加 enctype="multipart/form-data"
,否则您将无法使用 $_FILES
。
所以让它像:-
<form action="NewOwnedItem.php" method="POST" enctype="multipart/form-data">