I am using the code below to update an image and category in my database. The code updates the category name, but not the image. Its showing an error
Undefined index: picture in
C:\xampp\htdocs\project1\catalog\editcat.php on line 44
Where line 44 is:
$img=$_FILES['picture']['name'];
PHP:
<?php
$id=$_POST['categoryid'];
$name=$_POST['categoryname'];
$img=$_FILES['picture']['name'];
$target="upload";
$qry=mysql_query("UPDATE category SET categoryname='$name' WHERE category_id=$id");
if(!$qry)
{
die("Query Failed: ". mysql_error());
}?>
<?php
if($img)
{
$image= addslashes(file_get_contents($_FILES['picture']['name']));
$image_name= addslashes($_FILES['picture']['name']);
move_uploaded_file($_FILES["picture"]["name"],$target."/" . $_FILES["picture"]["name"]);
$location=$target."/". $_FILES["picture"]["name"];
//$name=$_POST['categoryname'];
$qry=mysql_query("UPDATE category SET uploadedfile='$location' WHERE category_id=$id");
}
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
?>
HTML:
<tr>
<td align="right" class="label" width="40%""><font color="blue">Category ID:</font> </td>
<td align ="left" width="60%"><input type="text" name="categoryid" id="categoryid" value="<?php echo $entries['category_id']; ?>" /></td>
</tr>
<tr>
<td align="right" class="label" width="40%""><font color="blue">Category Name:</font> </td>
<td align ="left" width="60%"><input type="text" name="categoryname" id="categoryname" value="<?php echo $entries['categoryname']; ?>" /></td>
</tr>
<tr><td align ="right" class="label" width="40%"><font color="blue">Upload File: </font></td>
<td align="left" width ="60%"> <input type="file" name="picture" ></td></tr>
<tr><td class="label" align="right"> </td><td align="left"><input type="submit" name="Submit" value="Upload" id="button1" /></td>
</tr>
Try the following solution. The reason behind it is that you move the file before assigning the location. It might clear $_FILES["picture"]["name"]
So what I suggest is is that you assign $img
after the post. Use that variable as it should not clear at any stage. Then when moving the file make use of the $_FILES['picture']['tmp_name']
.
move_uploaded_file($_FILES['picture']['tmp_name'],$target."/" . $img);
$location=$target."/". $img;
Please changes some line in your code:
//$image= addslashes(file_get_contents($_FILES['picture']['name']));
Above variable not in use
Changes this line
move_uploaded_file($_FILES["picture"]["name"],$target."/" . $_FILES["picture"]["name"]);
to
move_uploaded_file($_FILES["picture"]["tmp_name"],$target."/" . $image_name);
Make sure that you have add attribute in form if you are not using this your file not uploaded in your server.
enctype="multipart/form-data"
Here is code
if($img)
{
$image_name= addslashes($_FILES['picture']['name']);
move_uploaded_file($_FILES["picture"]["tmp_name"],$target."/" . $image_name);
$location=$target."/". $image_name;
//$name=$_POST['categoryname'];
$qry=mysql_query("UPDATE category SET uploadedfile='$location' WHERE category_id=$id");
}
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
?>