Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Multiple image uploader with update option

Codes here !

Moderators: macek, egami, gesf

Multiple image uploader with update option

Postby accend » Fri Mar 30, 2012 2:29 am

Hello,

I am really struggling to get something working and each time I think I get there I have to go back to the drawing board and start again, so in that if its OK I haven't got anything to show as I need new ideas or if possible the script itself if its already been done.

What I have is a form in a CMS where as well as all the other data the client has the option of uploading up to 4 images, they can upload 1, 2 3 or 4.

I actually have had this working, where the image goes to the server and the path to the database, but where I go wrong is when he comes to edit that piece of stock. Again as well as being able to edit anything on that item, I want to be able to update an image or add an extra one in, or if OK none of the images.

So basically on the edit bit, they can edit 1,2,3 or all 4 of the images, and if less than all 4 it doesn't impact on the other remaining images, they stay as they are, only the one they edited is upadted.

Can this be done, as I have been weeks on this, been to many forums and cant find any help at all.

Please, please help me.

OK this is the script Im using to do the first upload.

Code: Select all
$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;
}
}


And here is the images part of the form. There are 4 image upload options.

Code: Select all
<form name="form1" action="admin_stock.php?index=<?=$index?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="flag" value="<?= $flag?>">

<input type="file" name="pic1[]" id="pic1[]" size="50" />

<input type="submit" name="btnsubmit" value="Submit">
<input type="submit" name="btndelete" value="Delete" onClick="return check();">
<input type="hidden" name="srno1" value="<?= $rows["stock_Id"];?>">
<input type="hidden" name="action" value="Upload">


What I need help with is when the client clicks a stock item to edit it, the 4 image upload section is there and the client to update none of them or any of the 4, and if he doesnt change any then they stay as they are, or if he updates 1,2 or 3 then the remaining ones arent changed or deleted either, they stay as they are in the database.
accend
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Mar 19, 2012 7:48 am

Re: Multiple image uploader with update option

Postby egami » Fri Mar 30, 2012 7:29 am

I'm making a huge assumption here thinking that you're storing the image names in a different table than your user's information, and your stock information.

your stock information should hold the user_id value of the user.
Your image table should hold not only the user_id, but the stock id as well.

so the table should look a little something like this..

table: images
field names: id,datetime,user_id,stock_id,path,filename,picnumber

ie.. (assuming)
User_id = 1
Stock_id = 10

so you would do a query on the images table to see how many pictures the user has for this particular stock item and allow an upload of only the remainder if the total does not equal 4.

for example..
$query = "SELECT * FROM images WHERE stock_id='$stock_id' AND user_id='$user_id'";
$result = mysql_query($query);
$nums = mysql_num_rows($result);
$counted = 0;
while ($row = mysql_fetch_assoc($result)) {
$counted += 1;
echo "<span>Picture:</span><span>'.$row['filename'].'</span><span><a href="edit_pic.php?id='.$row['id'].'">Edit</a></span>'; // you'd have to create the edit capability for 1 picture only, by the picture ID #
}

for ($i = $counted; $i < 4; $i++) {
echo '<span><input type="file" name="file"></span>';
} // This creates a field to upload remaining pictures if there is less than 4. If there's 4 pictures, it won't create the form to upload anymore.


on the flip side of this..
on your edit form, you would re-capture all of the data for the picture and allow the user to either delete, or upload a new picture to replace the current and existing one.. in which case the picnumber value would stay the same because you're keeping the same ID, or you can change the count and the fields of the table as you go.. reset.. etc.
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Multiple image uploader with update option

Postby accend » Fri Mar 30, 2012 8:06 am

Hi there,

I am really grateful you have replied as nobody else has in a ny forum, so thank you.

I read your post again, and i have a table that contains all the information for one stock item, and there no user id as its for a shop owned by the client.

So basically only he can enter the admin area.

Also I think what you are saying is that they get 4 opportunities to upload stock in the first upload, then I think what your saying is that if all 4 images are up, then no more opportunities exist to upload mre images, when that isnt my idea. What I need to be able to do is upload UP TO 4 images, it could be 1,2,3 or 4 images, and then in the edit, they can eaither leave the images be as they happy that the correct images are up, or if at a later date he gets new images for the stock he can change either 1,2,3 or all 4 of the images. But lets say he has uploaded 4, and only wants to change 1 of them, he can update that pic and the remaining 3 stay as they are.

Does that make sense.
accend
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Mar 19, 2012 7:48 am

Re: Multiple image uploader with update option

Postby egami » Fri Mar 30, 2012 8:40 am

Sure, and that's exactly what I put up there.

What that does is basically as follows..

creates a list of pictures if they exist.. up to 4.
It lists the pictures, one by one, with an option to edit 1 picture at a time.
If there are less than 4, it has the option to load up however many are remaining.


Here's the problem.
You can't take the value of a picture and populate the "browse" file field.
So, if he uploads 4 pictures, and then decides to only change two... how is he supposed to know which one he wants changed?
-- Scenario one : He uploads 4 pictures again, and changes the two that he wants to change..
-- Scenario two: He deletes however many pictures he wants to delete, they re-order, and he uploads the remaining..

-- Scenario three: He does how I suggested above.
This is the best scenario, as he has control over which picture he's editing/deleting/changing without having to upload all 4 pictures again..
He can use the id field of the picture id to edit/delete
He can keep track of which files/images are associated to the stock item

There isn't much choice in it really..
No other way would make much sense without having to dump a bunch of Javascript/JQUERY into it to keep track of the numbers, but even if you do.. the PHP on the backside which handles the files and the names doesn't keep track of the id numbers associated.

How would you write that..
<input type="file" name="file"> contains nothing that points the picture id, of the table row.
See my point?

By posting four file upload input elements, each time the browser hits submit, the 1-4 will exist, just that however many names he skipped will be blank. This doesn't do much for you in the way of keeping track of the id of the picture.


Make sense?
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Multiple image uploader with update option

Postby accend » Fri Mar 30, 2012 8:57 am

Right yes I get you.

I thought it would be useful for you to see it, so I pm'd you with my admin login details.

I think what has happened her to me, is that I used this CMS to learn PHP & MySQL and i think I have done a half decent job, but then I have come to a very hard bit for me and my brain just couldnt work it out.

If I could or if you wanted me to I could send you the whole page and maybe you could help me implement your script into my work.

I'm so grateful for your help, as this is the last bit and its really been tough trying to work it out.

Code: Select all
<?php
include "config.php";
include "header2.php";

$flag=1;

$name="";
$meta="";
$ref="";
$desc="";
$maker="";
$date="";
$weight="";
$height="";
$depth="";
$width="";
$price="";
$sold="";
$conf ="";
$update="";
$pcats="";
$subcats="";
$dt2=date("Y-m-d H:i:s");

if(isset($_GET['stock_Id']))
{
   $stock_Id=$_GET['stock_Id'];
   $q=mysql_query("select * from stock where stock_Id=$stock_Id") or die (mysql_error());   
   $rows=mysql_fetch_assoc($q);
   $name=$rows["stock_Name"];
   $meta=$rows["stock_MetaTitle"];
   $pcats=$rows["parent_Category"];
   $subcats=$rows["sub_Category"];
   $ref=$rows["stock_Ref"];
   $desc=$rows["stock_Description"];
   $maker=$rows["stock_Maker"];
   $date=$rows["stock_Date"];
   $weight=$rows["stock_Weight"];
   $height=$rows["stock_Height"];
   $depth=$rows["stock_Depth"];
   $width=$rows["stock_Width"];
   $price=$rows["stock_Price"];
   $sold=$rows["stock_Sold"];
   $pic1=$rows["stock_Image"];
    $pic2=$rows["stock_Image2"];
    $pic3=$rows["stock_Image3"];
    $pic4=$rows["stock_Image4"];
   $active=$rows["stock_Active"];
   $time=$rows["stock_DateTime"];
   $flag=0;
}

if(isset($_POST['btndelete']))
{
   $sr=$_POST['srno1'];
   $flag=$_POST['flag'];
   if ($flag==0)
   {      
   $q23=mysql_query("delete from stock where stock_Id=$sr") or die (mysql_error());
   $flag=1;
   }
   else
   {      
   }
}

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

$flag=$_POST['flag'];
if ($flag==0)
{
//$stock_Id=$_GET['stock_Id'];
$sr=$_POST['srno1'];
$q=mysql_query("select stock_Image, stock_Image2, stock_Image3, stock_Image4 from stock where stock_Id=$sr") or die (mysql_error());
$rowsb=mysql_fetch_assoc($q);   
$name=$_POST['txtname'];
$desc1=$_POST['e1m1'];   
   
$pic1c="";
$pic2c="";
$pic3c="";
$pic4c="";
$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'];

$q24=mysql_query("update stock set stock_Name='$name', stock_MetaTitle='$meta', parent_Category='$pcats', sub_Category='$subcats', stock_Image='$pic[0]', stock_Image2='$pic[1]', stock_Image3='$pic[2]', stock_Image4='$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";
}
else
{
$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;
}
}
?>
<form name="form1" action="admin_stock.php?index=<?=$index?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="flag" value="<?= $flag?>">
<table align="center">
<tr>
<div style="position:relative; height:90px; width:896px; background-color:#293334; margin-bottom:0px; margin-left:auto; margin-right:auto;">
<div style="position:relative; height:55px; width:100%; font-size:20px; color:#FFFFFF; font-family:Verdana, Arial, Helvetica, sans-serif; margin-left:25px; top:9px; font-weight:bold;">Sovereign Antiques 'Stock' Management Section</div>
<div style="position:relative; height:15px; width:100%;">
<div style="position:relative; float:left; margin-left:25px;">
<a href="main.php" class="topNav" title="Home">Home</a> <span style="color:#FFFFFF">|</span> <a href="index-logOut.php" class="topNav" title="Log Out">Log Out</a> <span style="color:#FFFFFF">|</span> <a href="http://www.accendsandbox.co.uk" target="_blank" class="topNav" title="Log Out">View Website</a> <span style="color:#FFFFFF">|</span>
</div>
</div>
</div>
</tr>
<!--<tr class="f1">
<td class="f1"><?= $conf ?></td>
</tr>-->
</table>
<table align="center" width="900" border="0" cellpadding="5" cellspacing="2">
<tr>
<td colspan="2" bgcolor="#222222" height="45" style="background-image:url(images/admin_Grey_Bar_Pages.jpg)">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:13px; color:#ffffff; font-weight:bold; position:relative; float:left; left:20px; top:-2px;">Create/Edit - 'Stock'</div>
</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 Title:</div>
</td>            
<td bgcolor="#888888" width="713"><input type="text" name="txtname" size="84" value="<? echo $name;?>" /></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 Meta-Title:</div>
</td>            
<td bgcolor="#888888" width="713"><input type="text" name="txtmeta" size="84" value="<? echo $meta;?>" /></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;">Associated With:</div>
</td>
<td bgcolor="#888888" width="713">
<select name="pcats" size="1">
<option value="">Select Category</option>
<?php $qw=mysql_query("select cat_Id, category_Name from category order by category_Name ASC") or die (mysql_error());
while($r=mysql_fetch_assoc($qw))
   {
if($r['cat_Id']==$pcats)
   {
echo "<option value='$r[cat_Id]' selected>$r[category_Name]</option>";
   }
else
   {
echo "<option value='$r[cat_Id]'>$r[category_Name]</option>";
   }
}
?>
</select>
&nbsp; or &nbsp;
<select name="subcats" size="1">
<option value="">Select Sub-Category</option>
<?php $qw=mysql_query("select sub_Id, sub_Name from sub_category order by sub_Name ASC") or die (mysql_error());
while($r=mysql_fetch_assoc($qw))
   {
if($r['sub_Id']==$subcats)
   {
echo "<option value='$r[sub_Id]' selected>$r[sub_Name]</option>";
   }
else
   {
echo "<option value='$r[sub_Id]'>$r[sub_Name]</option>";
   }
}
?>
</select>
</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;">Reference Number:</div>
</td>            
<td bgcolor="#888888" width="713"><input type="text" name="Ref" size="25" value="<? echo $ref;?>" /></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;">Description:</div>
</td>
<td bgcolor="#888888" width="713"><textarea id="e1m1" name="e1m1" rows="25" cols="80" style="width:100%" class="mceEditor"><? echo $desc;?></textarea></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;">Maker:</div>
</td>            
<td bgcolor="#888888" width="713"><input type="text" name="Maker" size="84" value="<? echo $maker;?>" /> <span style="color:#990000 ; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11;">* Optional</span></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;">Date:</div>
</td>            
<td bgcolor="#888888" width="713"><input type="text" name="Maker" size="24" value="<? echo $date;?>" /> <span style="color:#990000 ; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11;">* Optional</span></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;">Weight:</div>
</td>            
<td bgcolor="#888888" width="713"><input type="text" name="Weight" size="24" value="<? echo $weight;?>" /> <span style="color:#990000 ; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11;">* Optional</span></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;">Height:</div>
</td>            
<td bgcolor="#888888" width="713"><input type="text" name="Height" size="24" value="<? echo $height;?>" /> <span style="color:#990000 ; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11;">* Optional</span></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;">Depth:</div>
</td>            
<td bgcolor="#888888" width="713"><input type="text" name="Depth" size="24" value="<? echo $depth;?>" /> <span style="color:#990000 ; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11;">* Optional</span></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;">Width:</div>
</td>            
<td bgcolor="#888888" width="713"><input type="text" name="Width" size="24" value="<? echo $width;?>" /> <span style="color:#990000 ; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11;">* Optional</span></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;">Price:</div>
</td>            
<td bgcolor="#888888" width="713"><input type="text" name="txtprice" size="24" value="<? echo $price;?>" /> <span style="color:#990000 ; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11;">(eg 1500 - No need for '£')</span></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;">Sold or For Sale:</div>
</td>
<td bgcolor="#888888" width="713">
<select name="txtsold" size="1">
<?php
if($sold==1)
{
echo "<option value='1' selected>Sold</option>";
echo "<option value='0'>For Sale</option>";                     
}
else
{
echo "<option value='1' >Sold</option>";
echo "<option value='0' selected>For Sale</option>";                     
}
?>
</select>
</td>
</tr>
<? if(isset($_GET['stock_Id']))
{ ?>
<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="pic2[]" value="<?php echo $pic1;?>" />&nbsp;<img src="<?php echo $pic1;?>" height="100px" />
</td>
</tr>
<? } else { ?>
<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>
<? } ?>

<? if(isset($_GET['stock_Id']))
{ ?>
<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="pic2[]" value="<?php echo $pic2;?>" />&nbsp;<img src="<?php echo $pic2;?>" height="100px" />
</td>
</tr>
<? } else { ?>
<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>
<? } ?>
<? if(isset($_GET['stock_Id']))
{ ?>
<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="pic3x" size="50" /> <input type="hidden" name="pic3" value="<?php echo $pic3;?>" /> &nbsp;<img src="<?php echo $pic3;?>" height="100px" />
</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>
</tr>
<? } else { ?>
<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>
<? } ?>
<? if(isset($_GET['stock_Id']))
{ ?>
<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="pic2[]" value="<?php echo $pic4;?>" />&nbsp;<img src="<?php echo $pic4;?>" height="100px" />
</td>
</tr>
<? } else { ?>
<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>
<? } ?>
<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 Active:</div>
</td>
<td bgcolor="#888888" width="713">
<select name="active" size="1">
<?php
if($active==1)
{
echo "<option value='1' selected>Active</option>";
echo "<option value='0'>Inactive</option>";                     
}
else
{
echo "<option value='1' >Active</option>";
echo "<option value='0' selected>Inactive</option>";                     
}
?>
</select>
</td>
</tr>

<tr>
<td bgcolor="#A0B050" width="161"><br></td>
<td bgcolor="#888888" width="713">
<table width="75%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="10%">
<input type="submit" name="btnsubmit" value="Submit">
<input type="submit" name="btndelete" value="Delete" onClick="return check();">
<input type="hidden" name="srno1" value="<?= $rows["stock_Id"];?>">
<input type="hidden" name="action" value="Upload">
</td>
</tr>
</table>
</td>
</tr>

<tr>
<td>&nbsp;</td>
</tr>

<tr>
<td valign="top" height="45" bgcolor="#eeeeee" width="161" style="background-image:url(images/admin_Grey_Bar_Smallest.jpg)">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:13px; color:#ffffff; font-weight:bold; position:relative; float:left; left:20px; top:7px;">Existing Stock</div>
</td>
<td bgcolor="#eeeeee" style="background-image:url(images/admin_Grey_Bar_509_No_Arrow.jpg)"><div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:13px; color:#777777; font-weight:normal; position:relative; float:left; left:20px; top:-2px; font-style:italic;">Select 'Existing Stock Name' below to edit or delete...</div></td>
</tr>   
<tr>
<td width="161" bgcolor="#A0B050" height="35">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#293334; font-weight:bold; position:relative; float:left; left:10px;">Stock Name</div></td>
<td width="713" bgcolor="#A0B050" height="35">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#293334; font-weight:bold; position:relative; float:left; left:10px;">Stock Description</div><div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:12px; color:#293334; font-weight:bold; position:relative; float:right; width:100px; right:10px">Last Updated</div></td>
</tr>
            
<?php
$q=mysql_query("select * from stock order by stock_Name ASC") or die (mysql_error());            
while($row=mysql_fetch_assoc($q))
{
echo "<tr>";
echo "<td bgcolor='#888888' width='161' valign='top'>";
echo "<p style='position:relative; left:9px;'><font size='2' color='#ffffff' face='Verdana,Arial,Helvetica,sans-serif'><a href='admin_stock.php?stock_Id=".$row["stock_Id"]."&index=".$index."' style='color:FFFFFF; font-weight:normal;'>".$row["stock_Name"]."</a></font></p>";
echo "</td>";
echo "<td bgcolor='#888888' width='713' valign='top'><div style='position:relative; float:left; width:550px;'><font size='2' color='#ffffff' face='Verdana,Arial,Helvetica,sans-serif' style='position:relative; left:9px;'>".substr($row['stock_Description'],0,61)."...</font></div><div style='position:relative; float:right; width:150px; right:-5px; border-left:#333333 dotted 1px; height:20px;'><font size='1' color='#ffffff' face='Verdana,Arial,Helvetica,sans-serif' style='position:relative; margin-left:10px;height:20px;top:3px;'>".$row["stock_DateTime"]."</font></div></td>";
echo "</tr>";
}            
?>
<tr>
<td>
<div style="position:relative; width:100%; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px">Return to <a href="#Top">TOP</a></div>
</td>
<tr>
</table>
</form>


I know its a lot, but I really would like your help.
accend
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Mar 19, 2012 7:48 am

Re: Multiple image uploader with update option

Postby egami » Fri Mar 30, 2012 10:30 am

My script won't exactly fit into your code, it was meant for you to get a general idea. I apologize, but I don't have much time to help in this capacity. I'm just lucky that today is a slow day for me.

But, there's a fellow that used to come on here quite a bit that I haven't seen in a while, but he's really good and might be able to help you.

Do a user search for Nullsig. PM him (which I hope goes to his E-Mail) and ask him. He might want a little bit of $ in return, but it'll be worth it for both of you in the end.
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Multiple image uploader with update option

Postby accend » Sat Mar 31, 2012 1:02 am

Cheers egami,

I have PM'd that person so will wait and see now if anything can be done.

I think I understand what your saying, but implementing it at this stage is above my skills, but I have learnt a lot.

Thanks again and take care
accend
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Mar 19, 2012 7:48 am

Re: Multiple image uploader with update option

Postby accend » Mon Apr 02, 2012 1:56 pm

Unfortunately I havent heared anything back from tha person just yet, and I am still looking for help if anybody can help me integrate the ideas posted into my page.

Thanks
accend
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Mar 19, 2012 7:48 am

Re: Multiple image uploader with update option

Postby Nullsig » Tue May 01, 2012 7:28 am

So from what I am seeing in the site from the info you gave me, and what I understand from your description your current problem is:

-You give the user the option to insert up to 4 images, none of which are required.

-You want the user to know they have uploaded images before so they can choose to update a previously added image or just add another one to an unused slot

-You want on update only the altered slots to be updated with new data

I only repeat this so that I can confirm what is going on and logically sort it out for myself.

So there are a few scenarios you need to cover:

--Scenario 1: Original Creation
This one is easy they are creating a stock and any images that have been loaded just get pushed into the database. No parsing or anything.

--Scenario 2: Additional Image Added
In this one you have loaded up an existing stock and want to add a picture to the list of X, where X is less than 4 images.

Here your only worry is that if you treat it like a full submission the old files will be overwritten as blank in the system. For example if you have 3 images uploaded and you add 1 more to it, when the submit goes off the 3 existing images have file submission boxes that are blank and those are overwritten to be empty while the fourth one is set up.


--Scenario 3: Editing an existing Image
Here you want to overwrite an image while preserving all other images. So overwriting one doesn't overwrite the other 3 as blank. You also need to make sure you are overwriting the correct image.



--My Observations about you page currently
Looking at your page you have already named the image boxes with different names. This is good. In situations where order doesn't matter you can use the array notation for these but here you actually need to know which ones exist and which do not.

Continuing on, You have the New Stock and Existing Stock utilizing the same form and you seem to be just per-populating the values. That's also fine. I like the fact that you are trying to show the images. The test one I created doesn't have anything so it should not try to display those images. [On my screen I see 4 broken links]

The easiest way to keep track of what exists and what doesn't is with a hidden variable or multiple hidden variables (it's a pick your poison sort of situation). Here I would put a hidden variable in the form that dictates if the value in the database for each variable exists or is empty.

Code: Select all
<input type="hidden" name="ckImg1" value="exists">

OR
Code: Select all
<input type="hidden" name="ckImg1" value="empty">


So on submission you know if the original value had something in it or not.


Now in your submission you are probably using the "INSERT... ON DUPLICATE KEY UPDATE" since I don't see a variance in the "action" hidden variable, but I could be missing something.

Unlike Egami, I am going to assume you are keeping all of this information on 1 table. If you aren't we can address that later.

The great thing about PHP is that you can include conditionals inline when constructing a query, something that is invaluable once you know how to do it.

The basic structure is:

Code: Select all
$query = (conditional) ? "value if true" : "value if false";


This can be done as part of a concatenation by enclosing everything after the "=" in parenthesis

So in this case you have the following basic query:

Code: Select all
$query = "INSERT INTO tblStocks VALUES (all of the submitted form values here)
              ON DUPLICATE KEY UPDATE";


Now for the fun part, you will conditionally concatenate the new image values based off the hidden checks.

Code: Select all
$query = "INSERT INTO tblStocks VALUES (all of the submitted form values here)
              ON DUPLICATE KEY UPDATE " .
              (($_POST['ckImg1'] == 'empty' || !empty($_POST['image1']) ? " image1 = '" . $_FILES['image1']['name'] . "' " : "");


You can repeat that process for all of the images and then continue on with updating things that don't need to be so conditionally aware.

OTHER NOTES:
Your form has no required fields so I, in the process of testing, added a couple of junk stocks.
User avatar
Nullsig
php-forum Fan User
php-forum Fan User
 
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron