提问人:Miff Rose 提问时间:1/4/2012 最后编辑:hakreMiff Rose 更新时间:8/1/2012 访问量:3215
尝试在 PHP 中使用 $_FILE 时出现“未定义索引”错误 [已关闭]
Getting 'undefined index' error while trying to use $_FILE in PHP [closed]
问:
我可以轻松地将文件上传到数据库中,但是当我想编辑文件时,我收到未定义的错误。代码如下:
product_update.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此文件采用 FORM 的所有值并更新数据库。其他字段已更新,但 FILE UPLOAD 不起作用。它给出错误。
<?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
答:
2赞
nikc.org
1/4/2012
#1
上传文件时,您必须为表单指定。enctype="multipart/form-data"
<form name="form1" method="post" action="update_post.php" enctype="multipart/form-data"/>
...
</form>
评论
0赞
Miff Rose
1/4/2012
哦,让我插入它并运行该文件。谢谢
0赞
Miff Rose
1/4/2012
你的伎俩奏效了!它正在运行!但是,现在,还有另一个问题。如果我只上传 1 个文件,那么只显示一个文件的名称。这是正确的。之后,如果我再上传 2 个文件,我没有碰过的上一条记录就会消失。为什么?–
0赞
nikc.org
1/4/2012
@MiffRose 可能是因为你奇怪的SQL语法。但这是一个不同的问题。一次只问一个问题,SO不是一个论坛。INSERT...WHERE
2赞
hakre
1/4/2012
@MiffRose:先阅读,再理解,再编码:php.net/manual/en/features.file-upload.php
3赞
Deleteman
1/4/2012
#2
您的表单需要该属性才能上传文件。enctype="multipart/form-data"
更多信息请点击此处
评论
0赞
Miff Rose
1/4/2012
我已经这样做了,它正在工作。但现在的另一个问题是,如果我不修改上传的文件,我如何将其保留在记录中?因为,目前可以选择上传 3 个文件。如果在 MOdifying 表单中,我只上传image_1,其余的我不触及,然后提交。结果是,上传文件的名称出现,而我没有编辑的上一条记录消失了。
下一个:PHP post 中未定义的索引
评论