Forum Moderators: coopster

Message Too Old, No Replies

Which is the best way to insert 6 pics?

         

Smad

3:01 pm on Jan 13, 2004 (gmt 0)

10+ Year Member



Hi all

I am still struggling with this CMS..but getting there :) anyway.. the CMS needs to have an admin section for inserting new records. This needs to include upto 6 images which i will be storing in the DB as path names using unique IDs.

My problem is..
P.S picture 1 needs to be thumbnailed aswell.
All must have restricted size and be jpgs.

Is it better to insert 1 at a time? if so how?

Is it better to insert all 6 via 6 browse fields in the form? if so will this potentially burn temp memory set at 8mb?

to be honest i am little lost to the best solution so any help here would be great..

TIA

jatar_k

2:45 am on Jan 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe try taking a look at this
Image upload and Thumbnail Generator [webmasterworld.com] msg 29 and 30

Smad

10:51 am on Jan 14, 2004 (gmt 0)

10+ Year Member



After reading that link i am still a tad confused although that did help some..

my table will have 6 fields, picture1, picture2, picture3 etc.

each one is populated with a unique ID for the image. example; uploads/029329743.jpg

I dont need to thumbnail these now as i can do that on the fly. But i would like help creating a while loop (i think) to insert each image name into the DB and upload each file to uploads/

Also this needs to be flexible just incase they dont add all the images, and possibly increase it from 6 at a later date..

Also how easy would it be to change an image after uploading and remove the old one at the same time?

Thanks

Smad

11:38 am on Jan 15, 2004 (gmt 0)

10+ Year Member



still cant get it to work ..what might help is if someone could explain what this does please and how important it is in that script? I think its checking the DB for exiting images names...

if ($_FILES['userfile']['name'][$q] == "") continue;
$num = $num + 1;
$sql = "SELECT pic_id FROM pics ORDER BY pic_id DESC LIMIT 1";
$result = mysql_query($sql);

big thanks for any help here its driving me mad

Smad

1:59 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



i am trying this step by step now and am using this snippet to work out a solution. The only problem is that it creates 6 unique file names even if you insert 1 image? Any ideas how to stop this please?

$uploaddir = 'uploads/';
$tot = count($userfile);
$num = 0;
for($q=0;$q<$tot;$q++){
print "".$tot."<br />";

$picture[$q] = $_FILES['userfile']['tmp_name'][$q];
print "".$picture[$q]."<br />";

$picture[$q] = "".uniqid(I).".jpg";
print "".$picture[$q]."<br />";

Smad

2:04 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



ignore last post sorry.. sorted now :D

coopster

2:04 pm on Jan 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



continue [php.net] is used within looping structures to skip the rest of the current loop iteration and continue execution at the beginning of the next iteration.

You must be uploading multiple files at the same time, therefore the

[$q]
index. The
if
statement is checking to see whether or not the original name of the file on the client machine was blank for that particular file on your form. If it is blank, skip over it as the user didn't browse their pc to pick up a file for that particular input field on the form.

Smad

2:36 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



This is my code now, but it doesnt insert into the DB. Does it matter that my DB fields are picture[0], picture[1], picture[2] etc?

$conn = db_connect();

// image uploader
$uploaddir = 'uploads/';
$tot = count($userfile);
$num = 0;
for($q=0;$q<$tot;$q++){

$picture[$q] = $_FILES['userfile']['tmp_name'][$q];
print "".$picture[$q]."<br />";

if ($_FILES['userfile']['name'][$q] == "") continue;
$num = $num + 1;
$picturename[$q] = "".uniqid(I).".jpg";
print "\$picturename = ".$picturename[$q]."<br />";

move_uploaded_file($_FILES['userfile']['tmp_name'][$q], $uploaddir . $picturename[$q]);
$filepath = 'uploads/' .$picturename[$q];
print $filepath;
$sql = "update brokerage
set 'picture[$q]' = '$filepath'
where id = 11";
$result = mysql_query($sql, $conn);
if (!$result) {
print "There was a database error when executing <pre>$sql</pre>";
print mysql_error();
exit;
}
}
@header('Location: '.$HTTP_POST_VARS['destination']);

results are as follows but i see now error :(

/tmp/php4PzRA9
$picturename = I4006a48a5b036.jpg
uploads/I4006a48a5b036.jpgThere was a database error when executing
update brokerage
set 'picture[0]' = 'uploads/I4006a48a5b036.jpg'
where id = 11
You have an error in your SQL syntax near ''picture[0]' = 'uploads/I4006a48a5b036.jpg' where id = 11' at line 2

Smad

4:56 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



ok i got it all working on its own but when i include the image upload part with my main script it ignores it :((

the if statement checks to see if its and update if not then this parts process... apart from the file uploader :/

else { // It's a new boat

$sql = "insert into brokerage
(type,price,make,model,year,engine_type,engine_hrs,fuel,LOA,beam,accommodation,brief,description,finance,warranty,date,maxspeed,lying)
values
('$type','$price','$make','$model','$year','$engine_type','$engine_hrs','$fuel','$LOA','$beam','$accommodation','$brief','$description','$finance','$warranty','$date','$maxspeed','$lying')";
$result = mysql_query($sql, $conn);
$boatID = mysql_insert_id();
$uploaddir = 'uploads/'; // image uploader
$tot = count($userfile);
$num = 0;
for($q=0;$q<$tot;$q++){
$picture[$q] = $_FILES['userfile']['tmp_name'][$q];

if ($_FILES['userfile']['name'][$q] == "") continue;
$picturename[$q] = "".uniqid(I).".jpg";

move_uploaded_file($_FILES['userfile']['tmp_name'][$q], $uploaddir . $picturename[$q]);
$filepath = 'uploads/' .$picturename[$q];
$imagesql = "update brokerage
set picture".$num." = '$filepath'
where id = $boatID";
$imageresult = mysql_query($imagesql, $conn);
$num = $num + 1;
}
}