Multiple image pploader with update option for all, some or none of the images

Title should read: Multiple image uploader with update option for all, some or none of the images

Hi all,

As you will see this is my first post in I really hope somebody can nail this for me, as I have been working on this for some time and I cannot get it to work.

Basically this is the final part of a CMS I’m building, and all was fine until I needed to build in an option of a multiple image uploader to accompany all other stock details.

I have managed to nail the ‘insert’ part of the multiple image uploader in this case up to 4 images, and the code is below for this part, any help with improvements though will be greatly appreciated.


if(isset($_POST['btnsubmit']))
{

$flag=$_POST['flag'];
if ($flag==0)
{

$name=$_POST['txtname'];
$desc1=$_POST['e1m1'];

$meta=$_POST['txtmeta'];
$sr=$_POST['srno1'];
$name=$_POST['txtname'];
$ref=$_POST['Ref'];
$desc=$_POST['e1m1'];
$maker=$_POST['Maker'];
$date=$_POST['Date'];
$weight=$_POST['Weight'];
$height=$_POST['Height'];
$depth=$_POST['Depth'];
$width=$_POST['Width'];
$price=$_POST['txtprice'];
$sold=$_POST['txtsold'];
$active=$_POST['active'];
$pcats=$_POST['pcats'];
$subcats=$_POST['subcats'];

$str_str='';
    $p='';
    $j=0;
    for($i=0;$i<=3;$i++){
    $j++;
    $p=$_REQUEST['p$j'];
    $file=$_FILES['pic']['name'][$i];

    if(!empty($file) ){
              $str_str.=",pic$j='$file'";
     }
     else if(!empty($p))$str_str.=",pic$j='$p'";

     $path1="imgdata/stock/".$file;
     copy($_FILES['pic']['tmp_name'][$i], $path1);

    }

$q24=mysql_query("update stock set stock_Name='$name', stock_MetaTitle='$meta', parent_Category='$pcats', sub_Category='$subcats', stock_Image='imgdata/stock/$pic[0]', stock_Image2='imgdata/stock/$pic[1]', stock_Image3='imgdata/stock/$pic[2]', stock_Image4='imgdata/stock/$pic[3]', stock_Ref='$ref', stock_Description='$desc1', stock_Maker='$maker', stock_Date='$date', stock_Weight='$weight', stock_Height='$height', stock_Depth='$depth',stock_Width='$width', stock_Price='$price', stock_Sold='$sold', stock_Active='$active', stock_DateTime='$dt2' where stock_Id=$sr") or die (mysql_error());
$flag=1;
$conf="Data Updated Successfully - Click <a href='http://www.accendsandbox.co.uk/adminSallam/admin_categories.php'>here</a> to continue";
$update="1";


<tr>
<td bgcolor="#A0B050" width="161">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#293334; font-weight:bold; position:relative; float:left; left:1px;">Stock Image 1 (Main):</div>
</td>
<td bgcolor="#888888">
<input type="file" name="pic1[]" id="pic1[]" size="50" />
</td>
</tr>
<tr>
<td bgcolor="#A0B050" width="161">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#293334; font-weight:bold; position:relative; float:left; left:1px;">Stock Image 2:</div>
</td>
<td bgcolor="#888888">
<input type="file" name="pic1[]" id="pic1[]" size="50" />
</td>
</tr>
<tr>
<td bgcolor="#A0B050" width="161">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#293334; font-weight:bold; position:relative; float:left; left:1px;">Stock Image 3:</div>
</td>
<td bgcolor="#888888">
<input type="file" name="pic1[]" id="pic1[]" size="50" />
</td>
</tr>
<tr>
<td bgcolor="#A0B050" width="161">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#293334; font-weight:bold; position:relative; float:left; left:1px;">Stock Image 4:</div>
</td>
<td bgcolor="#888888">
<input type="file" name="pic1[]" id="pic1[]" size="50" />
</td>
</tr>
<input type="submit" name="btnsubmit" value="Submit">
<input type="submit" name="btndelete" value="Delete" DEFANGED_OnClick="return check();">
<input type="hidden" name="srno1" value="<?= $rows["stock_Id"];?>">
<input type="hidden" name="action" value="Upload">

All seems to be fine above, I can upload 1,2,3 or 4 images and the image goes into the server and the path to the database.

But I then needed the option for my client to be able to click to edit a certain stock and then aswel as edit the other details, if he wanted to change one pic, no pics or all 4 he could and when he clicked submit, if there was a new image it would change if not it would stay as it is.

So here is my attempt and at the moment it doesnt work, so I’m looking for help of any kid and anything can change.


$name=$_POST['txtname'];
$ref=$_POST['Ref'];
$desc=$_POST['e1m1'];
$maker=$_POST['Maker'];
$date=$_POST['Date'];
$weight=$_POST['Weight'];
$height=$_POST['Height'];
$depth=$_POST['Depth'];
$width=$_POST['Width'];
$price=$_POST['txtprice'];
$sold=$_POST['txtsold'];
$meta=$_POST['txtmeta'];
$active=$_POST['active'];
$pcats=$_POST['pcats'];
$subcats=$_POST['subcats'];

$pic1='';
for($i=0;$i<4;$i++){
if(isset($_FILES['pic1']['name'][$i]))$pic1[$i]=$_FILES['pic1']['name'][$i];
else $pic1[$i]='';
}
for($i=0;$i<4;$i++){
if(isset($_FILES['pic1']['name'][$i]))$path1= "./imgdata/stock/".$_FILES['pic1']['name'][$i];
//echo $_FILES['pic1']['tmp_name'][$i]." :". $path1;
if(!empty($_FILES['pic1']['name'][$i])&&isset($_FILES['pic1']['name'][$i]))copy($_FILES['pic1']['tmp_name'][$i], $path1);
}

$q=mysql_query("insert into stock (stock_Name, stock_MetaTitle, parent_Category, sub_Category, stock_Ref, stock_Description, stock_Maker, stock_Date, stock_Weight, stock_Height, stock_Depth, stock_Width, stock_Price, stock_Sold, stock_Image, stock_Image2, stock_Image3, stock_Image4, stock_Active, stock_DateTime) values('$name','$meta','$pcats','$subcats','$ref','$desc','$maker','$date','$weight','$height','$depth','$width','$price','$sold','imgdata/stock/$pic1[0]','imgdata/stock/$pic1[1]','imgdata/stock/$pic1[2]','imgdata/stock/$pic1[3]','$active','$dt2')") or die (mysql_error());
$conf="Data Inserted Successfully - Click <a href='http://www.accendsandbox.co.uk/adminSallam/admin_stock.php'>here</a> to continue";
$update=1;


<tr>
<td bgcolor="#A0B050" width="161">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#293334; font-weight:bold; position:relative; float:left; left:1px;">Stock Image 1 (Main):</div>
</td>
<td bgcolor="#888888">
<input type="file" name="pic[]" size="50" /> <input type="hidden" name="p1" value="<?php echo $pic1;?>" /> &nbsp;<img src="<?php echo $pic1;?>" height="100px" />
</td>
</tr>
<tr>
<td bgcolor="#A0B050" width="161">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#293334; font-weight:bold; position:relative; float:left; left:1px;">Stock Image 2:</div>
</td>
<td bgcolor="#888888">
<input type="file" name="pic[]" size="50" /> <input type="hidden" name="p2" value="<?php echo $pic2;?>" /> &nbsp;<img src="<?php echo $pic2;?>" height="100px" />
</td>
</tr>
<tr>
<td bgcolor="#A0B050" width="161">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#293334; font-weight:bold; position:relative; float:left; left:1px;">Stock Image 3:</div>
</td>
<td bgcolor="#888888">
<input type="file" name="pic[]" size="50" /> <input type="hidden" name="p3" value="<?php echo $pic3;?>" /> &nbsp;<img src="<?php echo $pic3;?>" height="100px" />
</td>
</tr>
<tr>
<td bgcolor="#A0B050" width="161">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#293334; font-weight:bold; position:relative; float:left; left:1px;">Stock Image 4:</div>
</td>
<td bgcolor="#888888">
<input type="file" name="pic[]" size="50" /> <input type="hidden" name="p4" value="<?php echo $pic4;?>" /> &nbsp;<img src="<?php echo $pic4;?>" height="100px" />
</td>
</tr>

What happens above as its all on the same page, is when a stock item is selected to be edited the form to update the images changes to the image upload options above, instead of the original ones for a new stock item at the top of this post.

I can provide anything you need to help me with this, so please can somebody take a look and see if it can be got working, as its been a long problem for me this.

Cheers

Ok I have tried to narrow it down and have done i think, and would like some help with the code below to get it to work, if possible.

In the 1st upload all the stocks details go into the database fine, and the images too.

So then in the update I have a different set up for the images, and want some help to be able to update an image if it has been edited, if not it stays as is.

So below I have a type ‘file’ and type ‘hidden’ the hidden type being a reference to whats currently in the database.


<td bgcolor="#888888">
<input type="file" name="pic[]" size="50" /> <input type="hidden" name="pic2[]" value="<?php echo $pic1;?>" />&nbsp;<img src="<?php echo $pic1;?>" height="100px" />
</td>
<td bgcolor="#888888">
<input type="file" name="pic[]" size="50" /> <input type="hidden" name="pic2[]" value="<?php echo $pic2;?>" />&nbsp;<img src="<?php echo $pic2;?>" height="100px" />
</td>
<td>
<input type="file" name="pic[]" size="50" /> <input type="hidden" name="pic2[]" value="<?php echo $pic3;?>" />&nbsp;<img src="<?php echo $pic3;?>" height="100px" />
</td>
<td bgcolor="#888888">
<input type="file" name="pic[]" size="50" /> <input type="hidden" name="pic2[]" value="<?php echo $pic4;?>" />&nbsp;<img src="<?php echo $pic4;?>" height="100px" />
</td>

The code below can deal with the images if they are updated but cannot leave the others be if they arent edited and basically clears out whats in the database leaving it empty, so could somebody help me to be able to manage this.


$pic='';
for($i=0;$i<4;$i++){
if(isset($_FILES['pic']['name'][$i]))$pic[$i]=$_FILES['pic']['name'][$i];
else $pic[$i]='';
}
for($i=0;$i<4;$i++){
if(isset($_FILES['pic']['name'][$i]))$path= "./imgdata/stock/".$_FILES['pic']['name'][$i];
//echo $_FILES['pic1']['tmp_name'][$i]." :". $path1;
if(!empty($_FILES['pic']['name'][$i])&&isset($_FILES['pic']['name'][$i]))copy($_FILES['pic']['tmp_name'][$i], $path);
}

Cheers

I can provide more info if needed