Hi Dan,
> if i use this
> $query = "insert into images (pic) values ('p$i')"
> it doesnt seem to work just inserts it as Array0 Array 1 etc ..
I know. I'd actually expect it to insert "p1", "p2", etc... I just wanted
to insert SOMETHING into the database.
The reason your commented out line didn't work is because you didn't put a
dollar sign in front of "pic_name". Check it out:
> //$query = "insert into images (pic) values ('pic_name[$i]')";
Look at your query for iteration 1:
> [insert into images (pic) values ('delrow_focus.gif' 'delcol.gif' )]
Do you really want to try to insert two values into one column? Especially
since you just inserted the first one in iteration 0?
Please let me know what it is you're trying to do, since the code you're
writing doesn't match the impression that I get. I thought I've written
enough about appending the next file name to the list of previous ones and
rerunning the queries to stop you from doing that if it's not what you
intended.
If you're uploading 5 files, and inserting the filenames of those 5 files
into a database, each filename in its own row in the table, then you shoult
NOT be appending to the end of $values or $cols because you're going to be
reinserting a whole lot of stuff.
In effect, what you're doing should be extremely simple, and could be solved
with the simplest of loops:
for ($i = 0; $i < $nPics; $i++)
mysql_query("INSERT INTO images (pic) VALUES ($pic_name[$i])") );
Nik
-----Original Message-----
From: Dan Rossi [mailto:danr@s...]
Sent: Thursday, December 20, 2001 7:35 PM
To: professional php
Subject: [pro_php] RE: multiple upload and insert
ok heres wot i got output with
0:
query [insert into images (pic) values ('delrow_focus.gif' )]
returns => 1
----------------------------------------------------------------------------
----
1:
query [insert into images (pic) values ('delrow_focus.gif' 'delcol.gif' )]
returns => 1
using this
$db = DB::connect( "mysql://$dbuser:$dbpass@$dbhost/$dbname" );
$nPics = count($pic);
echo "<pre>";
for($i = 0; $i < $nPics; $i++)
{
//do stuff with $pic[$i] and $pic_name[$i] here..
$cols.="pic ";
$values.="'$pic_name[$i]' ";
//$query = "insert into images (pic) values ('pic_name[$i]')";
$query="insert into images (pic) values ($values)";
echo "$i:\n query [$query]\n returns => " .
$result = $db->query($query) . "\n<hr>\n";
}
echo "</pre>";
if i use this
$query = "insert into images (pic) values ('p$i')"
it doesnt seem to work just inserts it as Array0 Array 1 etc ..
-----Original Message-----
From: Nikolai Devereaux [mailto:yomama@u...]
Sent: Friday, December 21, 2001 11:05 AM
To: professional php
Subject: [pro_php] RE: multiple upload and insert
[ Prepare yourself, this is a long one... =) ]
Wait a minute!
I just noticed some things in your code. I don't think your query is at all
correct, or that you're using the db well.
[ Here's your code with line numbers followed by more comments: ]
1 $numPics = count($pic);
2 for($i = 0; $i < $numPics; $i++)
3 {
4 //do stuff with $pic[$i] and $pic_name[$i] here..
5 $db = DB::connect( "mysql://$dbuser:$dbpass@$dbhost/$dbname" );
6 $cols.=",pic ";
7 $values.=", '$pic_name[$i]' ";
8 $sql="insert into images (title[$i] $cols) values ('$title[$i]'
$values)";
9 $result = $db->query($sql);
10 }
[ Comments by line number: ]
5: You shouldn't have to reconnect to the DB every iteration of the loop.
Just do it once.
6 & 7: You're APPENDING to the end of $cols and $values! If they're
defined before the loop starts, you're continuously growing the value of
these strings, which I can't imagine would be the right thing to do, since
your mysql db probably only has a couple columns in it. Plus, since you're
continually adding ", pic" to the end of $cols, the second time you do the
query, you're going to try to
insert into images(title[1], pic, pic) ...
which mysql will NOT know how to handle correctly, i assume. Similarly,
you're appending the next filename to the end of the string with all the
previous ones.
8: What is "title[$i]" supposed to be? Do you really have columns in your
db named "title[0]", "title[1]", ...? I doubt it.
Simplify your loop as much as possible. I don't see why you're creating a
$cols variable within the scope of the loop, appending ",pic " to it. That
doesn't seem right.
If $cols or $values exist before the loop starts, your query will end up
looking like this:
Iteration $i = 0:
insert into images(title[0],pic ) values ('something0, file0 )
Iteration $i = 4:
insert into images(title[4],pic, pic ,pic ,pic ,pic)
values ('something4', file0 , file1 , file2 , file3 , file4 )
9: You're allocating a variable and it's required storage for $result, but
never using it. Just run the query if you don't plan to test the $result.
Better yet, you should really check to see if the result is valid and output
the error message when it's not. That will give you a pretty decent idea as
to why mysql is failing.
[ suggestions ]
Please post your db schema so I can see what mysql expects.
put a bunch of debug statements in there.
'Starting iteration ' . $i; // is a good one.
'$cols is "' . $cols . '"'; // is another.
'$values is "' . $values . '"'; // is another.
'$sql is "' . $sql . '"'; // is another.
'$result is "' . $result . '"'; // is another.
Good luck. Take a deep breath, step back from your code. Grab a beer.
Simplify your code. Don't use variables if you don't need to -- it's a
waste of space, and variable substitution takes time. It's not the best
idea to have a lot of time-expensive code within loops.
Also, use single-quoted strings if you're not planning on doing variable
substitution, it's a little faster.
[ let's take a break for a sec... ]
sigh...
[ okay, back to work... ]
try something super simple like this:
$conn = mysql_pconnect($user, $pass, $db);
$nPics = count($pic);
echo "<pre>";
for($i = 0; $i < $nPics; $i++)
{
$query = "insert into images (title, pic) values ('t$i', 'p$i')" )
echo "$i:\n query [$query]\n returns => " .
mysql_query($query) . "\n<hr>\n";
}
echo "</pre>";
Take care, best of luck
nik
-----Original Message-----
From: Dan Rossi [mailto:danr@s...]
Sent: Thursday, December 20, 2001 3:12 PM
To: professional php
Subject: [pro_php] RE: multiple upload and insert
yes i have an error msg thing setup i'll copy and paste it soon, oh the
multiple upload part works fine , there is dozens of examples to prove that
, but there isnt anything that involves upload and insert at the same time
-----Original Message-----
From: Nikolai Devereaux [mailto:yomama@u...]
Sent: Friday, December 21, 2001 7:01 AM
To: professional php
Subject: [pro_php] RE: multiple upload and insert
In addition to running the query you should print your queries and the db
result (and error, if any) to the screen to see exactly what's going on.
Also, verify that all the files you've moved from temp/$pic to
your_path/$pic_name exist in your_path/.
I can't play with your code yet, since it would take too long to get into it
and I'm at work... but when in doubt, output stuff to the screen for
debugging. =)
nik
-----Original Message-----
From: Dan Rossi [mailto:danr@s...]
Sent: Wednesday, December 19, 2001 3:36 PM
To: professional php
Subject: [pro_php] RE: multiple upload and insert
any chance of helping out here ?
-----Original Message-----
From: Nikolai Devereaux [mailto:yomama@u...]
Sent: Wednesday, December 19, 2001 3:13 PM
To: professional php
Subject: [pro_php] RE: multiple upload and insert
Okay, what are you trying to insert into the database?
The file, or the filename?
-----Original Message-----
From: Dan Rossi [mailto:danr@s...]
Sent: Tuesday, December 18, 2001 8:07 PM
To: professional php
Subject: [pro_php] RE: multiple upload and insert
unfortunatly this doesnt work
$numPics = count($pic);
for($i = 0; $i < $numPics; $i++)
{
//do stuff with $pic[$i] and $pic_name[$i] here..
$db = DB::connect( "mysql://$dbuser:$dbpass@$dbhost/$dbname" );
$cols.=",pic ";
$values.=", '$pic_name[$i]' ";
$sql="insert into images (title[$i] $cols) values ('$title[$i]'
$values)";
$result = $db->query($sql);
}
-----Original Message-----
From: Nikolai Devereaux [mailto:yomama@u...]
Sent: Wednesday, December 19, 2001 11:16 AM
To: professional php
Subject: [pro_php] RE: multiple upload and insert
I have several questions about your code. Here it is again for reference:
while(list($key) = each($pic))
{
copy($pic[$key],"../images/galleries/testion/".$pic_name[$key]);
$cols.=",pic ";
$values.=", '$pic_name[$key]' ";
$sql="insert into images (gallery $cols) values ('$gallery' $values)";
copy($uploadedfile[$key], "/directory/" . $uploadedfile_name($key));
unlink($pic[$key]);
next($pic[$key]);
}
1) Let's start with variables...
I'm assuming $gallery is defined somewhere else.
And HOPEFULLY, $uploadedfile and $uploadedfile_name are initialized as
well... you're sending your files to the server as $pic[], $pic_name[],
$pic_size[], and $pic_type[], not $uploadedfile[], $uploadedfile_name[],
etc...
2) What are you inserting into the DB?
It looks like all you're inserting is the filename, not the actual file
data. Hope this is what you want.
3) What are you deleting (unlinking)?
You're unlinking $pic[$key], which doesn't look right. Unlink() expects the
filename, which in your case should be $pic_name[something]. I don't think
that $key is the right replacement for "something". I wouldn't unlink() the
temp file that the server stores for an uploaded file. Instead, I'd move
the uploaded file from the temp storage to where you want it.
4) Don't mess with the internal array pointer!
Don't use next() at the end of your loop. You're modifying the internal
array pointer by doing so. Each() already does this for you.
Call reset($pic) before you enter the while loop, too.
5) What is $pic? What value do you expect to get for $key?
$pic is an array containing the "fake" filenames that the webserver assigned
to the uploaded files when it saved them in temp storage.
So $pic[$key] makes no sense to me. $key should be $pic[0] on the first
iteration, so what the heck does "$pic[$pic[0]]" mean?
The code in the while() test suggests that $key is going to be the filename
the webserver assigned to the file when it saved it in temp storage. But
then on the next line, you use $key as if it were a string or an integer
index into the $pic array, which it is not.
Here's a tip: If you're traversing an array using while(), list(), and
each(), you NEVER EVER EVER access the original array within the body of the
loop! You should only perform operations on the item you pulled while
iterating in the while test.
Pick better variable names, I guess.
Try changing your inputs to
<input type="file" name="pics[0]" />
<input type="file" name="pics[1]" />
...
<input type="file" name="pics[4]" />
and then
reset($pics);
while( list($pic) = each($pics) )
{
do stuff to $pic.
}
Or, since you have two parallel arrays (pic[] and pic_name[])
$numPics = count($pic);
for($i = 0; $i < $numPics; $i++)
{
do stuff with $pic[$i] and $pic_name[$i] here.
}
You're on this track with your other code snippet:
if (is_uploaded_file($pic[$i]))
{
copy($pic[$i],"../images/galleries/".$gallery."/".$pic_name[$i]);
$cols.=",pic ";
$values.=", '$pic_name[$i]' ";
$sql="insert into images (gallery[$i] $cols) values ('$gallery[$i]'
$values)";
}
Good luck, let me know how it goes.
Nik
-----Original Message-----
From: Dan Rossi [mailto:danr@s...]
Sent: Tuesday, December 18, 2001 3:23 PM
To: professional php
Subject: [pro_php] RE: multiple upload and insert
i have a few
while(list($key) = each($pic)) {
copy($pic[$key],"../images/galleries/testion/".$pic_name[$key]);
$cols.=",pic ";
$values.=", '$pic_name[$key]' ";
$sql="insert into images (gallery $cols) values ('$gallery'
$values)";
copy($uploadedfile[$key], "/directory/" . $uploadedfile_name($key));
unlink($pic[$key]);
next($pic[$key]);
}
if (is_uploaded_file($pic[$i])) {
copy($pic[$i],"../images/galleries/".$gallery."/".$pic_name[$i]);
$cols.=",pic ";
$values.=", '$pic_name[$i]' ";
$sql="insert into images (gallery[$i] $cols) values ('$gallery[$i]'
$values)";
}
they are using arrays
<input type=file name=\"pic[0]\">
<input type=file name=\"pic[1]\"> etc ..
-----Original Message-----
From: Adam Lang [mailto:aalang@r...]
Sent: Wednesday, December 19, 2001 12:58 AM
To: professional php
Subject: [pro_php] RE: multiple upload and insert
show your code
Adam Lang
Systems Engineer
Rutgers Casualty Insurance Company
http://www.rutgersinsurance.com
----- Original Message -----
From: "Dan Rossi" <danr@s...>
To: "professional php" <pro_php@p...>
Sent: Monday, December 17, 2001 7:22 PM
Subject: [pro_php] RE: multiple upload and insert
hi is there any possible way of doing a multiple upload and multiple
insert into the database at the same time , my scenario is i am trying
to allow multiple upload of images and at the same time i need to insert
them into the database , say i have 5 images i want to upload with a
name and comment field , i can upload them fine but doing a loop to
insert them into the database i am having problems with any suggestions?