Получение ошибки «undefined index» при попытке использовать $ _FILE в PHP

Я могу легко загрузить файлы в базу данных. Но когда я хочу редактировать файлы, я получаю Undefined error. Вот код:

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 и ОБНОВЛЯЕТ базу данных. Другие поля обновляются, НО ЗАГРУЗКА ФАЙЛА не работает. Это дает ошибку.

 <?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" .

Дополнительная информация здесь

При загрузке файлов вам нужно указать enctype="multipart/form-data" для формы.

 <form name="form1" method="post" action="update_post.php" enctype="multipart/form-data"/> ... </form>