By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,775 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,775 IT Pros & Developers. It's quick & easy.

acquiring value of primary key from uploaded file

P: n/a
I have a PHP file upload feature that also gives the user the option to
assign more than 1 project association to the file. The file's name, title,
primary project, etc. is inserted into a document link table in a MySQL db
after the file is uploaded. The secondary (and multiple) project(s) are also
selected using a separate list box (there are 2 boxes - one for the primary
project, and one for any/all associated projects) The selected values from
the secondary box need to be inserted into a junction table that reflects
both the id for the newly uploaded file and the secondary project(s) - with
a primary key for the association itself, i.e,:

assoc_id doc_id proj_id
1 265 5
2 265 3
3 265 1

I would use a foreach statement to pull the projects from the select list,
but how do I get the newly generated doc_id in order to pass that value to
the association table? Can I assign it a variable at the same time I insert
the data into the primary table? The ID is auto-incremented and is inserted
into the primary table as follows:

$tool_insert = "INSERT INTO tools VALUES ('', '$uploadedname', '$tooltitle',
'$tooldesc', '$tooltype_id', '$toolcat_id', '$primeproj_id', '$location')";

Thanks for any help.

Christina
Jul 17 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a

Chris wrote:
I have a PHP file upload feature that also gives the user the option to
assign more than 1 project association to the file. The file's name, title,
primary project, etc. is inserted into a document link table in a MySQL db
after the file is uploaded. The secondary (and multiple) project(s) are also
selected using a separate list box (there are 2 boxes - one for the primary
project, and one for any/all associated projects) The selected values from
the secondary box need to be inserted into a junction table that reflects
both the id for the newly uploaded file and the secondary project(s) - with
a primary key for the association itself, i.e,:

assoc_id doc_id proj_id
1 265 5
2 265 3
3 265 1

I would use a foreach statement to pull the projects from the select list,
but how do I get the newly generated doc_id in order to pass that value to
the association table? Can I assign it a variable at the same time I insert
the data into the primary table? The ID is auto-incremented and is inserted
into the primary table as follows:

$tool_insert = "INSERT INTO tools VALUES ('', '$uploadedname', '$tooltitle',
'$tooldesc', '$tooltype_id', '$toolcat_id', '$primeproj_id', '$location')";

Thanks for any help.

Christina
I believe you're looking for mysql_last_insert_id()

Frizzle.

Jul 17 '06 #2

P: n/a
Thanks - that makes sense, I'll give it a try. Hard to find all the
built-in functions - I had seen mysql_insert_id, but hadn't seen this one.

"frizzle" <ph********@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
>
I believe you're looking for mysql_last_insert_id()

Frizzle.

Jul 18 '06 #3

P: n/a
Rik
Chris wrote:
>I believe you're looking for mysql_last_insert_id()
Thanks - that makes sense, I'll give it a try. Hard to find all the
built-in functions - I had seen mysql_insert_id, but hadn't seen this
one.

That's because mysql_last_insert_id() is not a PHP function. Frizzle was
unfortunately on the fritz. mysql_insert_id() it is, which should work

Grtz,
--
Rik Wasmus
Jul 18 '06 #4

P: n/a

Rik wrote:
Chris wrote:
I believe you're looking for mysql_last_insert_id()
Thanks - that makes sense, I'll give it a try. Hard to find all the
built-in functions - I had seen mysql_insert_id, but hadn't seen this
one.


That's because mysql_last_insert_id() is not a PHP function. Frizzle was
unfortunately on the fritz. mysql_insert_id() it is, which should work

Grtz,
--
Rik Wasmus
I assume the result to be equal.
Would mysql_insert_id() still require a DB connection to be open?

Frizzle.

Jul 20 '06 #5

P: n/a
Rik
frizzle wrote:
Rik wrote:
>Chris wrote:
>>>I believe you're looking for mysql_last_insert_id()

Thanks - that makes sense, I'll give it a try. Hard to find all the
built-in functions - I had seen mysql_insert_id, but hadn't seen
this one.


That's because mysql_last_insert_id() is not a PHP function. Frizzle
was unfortunately on the fritz. mysql_insert_id() it is, which
should work

Grtz,
--
Rik Wasmus

I assume the result to be equal.
Would mysql_insert_id() still require a DB connection to be open?
Yes. It's result isn't cached after a query in PHP, but queried from the
database itself.

Grtz,
--
Rik Wasmus
Jul 20 '06 #6

P: n/a
frizzle wrote:
Rik wrote:
>>Chris wrote:
>>>>I believe you're looking for mysql_last_insert_id()
Thanks - that makes sense, I'll give it a try. Hard to find all the
built-in functions - I had seen mysql_insert_id, but hadn't seen this
one.


That's because mysql_last_insert_id() is not a PHP function. Frizzle was
unfortunately on the fritz. mysql_insert_id() it is, which should work

Grtz,
--
Rik Wasmus


I assume the result to be equal.
Would mysql_insert_id() still require a DB connection to be open?

Frizzle.
Yes, it's a MySQL call.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 20 '06 #7

P: n/a

Jerry Stuckle wrote:
frizzle wrote:
Rik wrote:
>Chris wrote:

I believe you're looking for mysql_last_insert_id()
Thanks - that makes sense, I'll give it a try. Hard to find all the
built-in functions - I had seen mysql_insert_id, but hadn't seen this
one.
That's because mysql_last_insert_id() is not a PHP function. Frizzle was
unfortunately on the fritz. mysql_insert_id() it is, which should work

Grtz,
--
Rik Wasmus

I assume the result to be equal.
Would mysql_insert_id() still require a DB connection to be open?

Frizzle.

Yes, it's a MySQL call.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
So you'd have to run another extra call to the database, instead of
getting it from cache ?
Wouldn't that make it slower?
Not quite getting this, but liked to know how it works ...

Frizzle

Jul 20 '06 #8

P: n/a
frizzle wrote:
Jerry Stuckle wrote:
>>frizzle wrote:
>>>Rik wrote:
Chris wrote:
>>I believe you're looking for mysql_last_insert_id()
>>
>
>Thanks - that makes sense, I'll give it a try. Hard to find all the
>built-in functions - I had seen mysql_insert_id, but hadn't seen this
>one.
That's because mysql_last_insert_id() is not a PHP function. Frizzle was
unfortunately on the fritz. mysql_insert_id() it is, which should work

Grtz,
--
Rik Wasmus
I assume the result to be equal.
Would mysql_insert_id() still require a DB connection to be open?

Frizzle.

Yes, it's a MySQL call.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================


So you'd have to run another extra call to the database, instead of
getting it from cache ?
Wouldn't that make it slower?
Not quite getting this, but liked to know how it works ...

Frizzle
Yes, because it isn't returned so there's no cache to store it in.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 20 '06 #9

P: n/a
On 20 Jul 2006 14:36:19 -0700, "frizzle" <ph********@gmail.comwrote:
>>I assume the result to be equal.
Would mysql_insert_id() still require a DB connection to be open?

Yes, it's a MySQL call.

So you'd have to run another extra call to the database, instead of
getting it from cache ?
Wouldn't that make it slower?
Not quite getting this, but liked to know how it works ...
Interesting question, as the docs don't seem to explicitly state whether it
does or does not.

Tracing network traffic with Ethereal, MySQL client 5.0.22, server version
4.1.7 on another machine, and sleep() calls between each step to make it
obvious what is happening at each stage, shows that calling mysql_insert_id()
does not result in any communication with the server; so the value must be sent
across and stored in the client as part of running the previous mysql_query().

Looking at the individual packets, this is indeed the case; the ID is returned
as part of the payload in the "OK" packet in reply to the query:
No. Time Source Destination Protocol Info
12 0.006948 192.168.1.101 192.168.1.100 MySQL Request
Command: Query : insert into t (id) values (null)

Frame 12 (91 bytes on wire, 91 bytes captured)
Ethernet II, Src: SC&C_d5:81:1c (00:00:21:d5:81:1c), Dst: SC&C_d5:81:1a
(00:00:21:d5:81:1a)
Internet Protocol, Src: 192.168.1.101 (192.168.1.101), Dst: 192.168.1.100
(192.168.1.100)
Transmission Control Protocol, Src Port: 3693 (3693), Dst Port: 3306 (3306),
Seq: 86, Ack: 83, Len: 37
MySQL Protocol
Packet Length: 33
Packet Number: 0
Command
Command: Query (3)
Parameter: insert into t (id) values (null)

No. Time Source Destination Protocol Info
13 0.008511 192.168.1.100 192.168.1.101 MySQL Response
OK

Frame 13 (65 bytes on wire, 65 bytes captured)
Ethernet II, Src: SC&C_d5:81:1a (00:00:21:d5:81:1a), Dst: SC&C_d5:81:1c
(00:00:21:d5:81:1c)
Internet Protocol, Src: 192.168.1.100 (192.168.1.100), Dst: 192.168.1.101
(192.168.1.101)
Transmission Control Protocol, Src Port: 3306 (3306), Dst Port: 3693 (3693),
Seq: 83, Ack: 123, Len: 11
MySQL Protocol
Packet Length: 7
Packet Number: 1
Response Code: 0
Payload: \001\017\002
In this case the ID was 15 - which is octal \017 - the middle byte in the
payload of the response to the query.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Jul 20 '06 #10

P: n/a

Andy Hassall wrote:
On 20 Jul 2006 14:36:19 -0700, "frizzle" <ph********@gmail.comwrote:
>I assume the result to be equal.
Would mysql_insert_id() still require a DB connection to be open?

Yes, it's a MySQL call.
So you'd have to run another extra call to the database, instead of
getting it from cache ?
Wouldn't that make it slower?
Not quite getting this, but liked to know how it works ...

Interesting question, as the docs don't seem to explicitly state whether it
does or does not.

Tracing network traffic with Ethereal, MySQL client 5.0.22, server version
4.1.7 on another machine, and sleep() calls between each step to make it
obvious what is happening at each stage, shows that calling mysql_insert_id()
does not result in any communication with the server; so the value must be sent
across and stored in the client as part of running the previous mysql_query().

Looking at the individual packets, this is indeed the case; the ID is returned
as part of the payload in the "OK" packet in reply to the query:
No. Time Source Destination Protocol Info
12 0.006948 192.168.1.101 192.168.1.100 MySQL Request
Command: Query : insert into t (id) values (null)

Frame 12 (91 bytes on wire, 91 bytes captured)
Ethernet II, Src: SC&C_d5:81:1c (00:00:21:d5:81:1c), Dst: SC&C_d5:81:1a
(00:00:21:d5:81:1a)
Internet Protocol, Src: 192.168.1.101 (192.168.1.101), Dst: 192.168.1.100
(192.168.1.100)
Transmission Control Protocol, Src Port: 3693 (3693), Dst Port: 3306 (3306),
Seq: 86, Ack: 83, Len: 37
MySQL Protocol
Packet Length: 33
Packet Number: 0
Command
Command: Query (3)
Parameter: insert into t (id) values (null)

No. Time Source Destination Protocol Info
13 0.008511 192.168.1.100 192.168.1.101 MySQL Response
OK

Frame 13 (65 bytes on wire, 65 bytes captured)
Ethernet II, Src: SC&C_d5:81:1a (00:00:21:d5:81:1a), Dst: SC&C_d5:81:1c
(00:00:21:d5:81:1c)
Internet Protocol, Src: 192.168.1.100 (192.168.1.100), Dst: 192.168.1.101
(192.168.1.101)
Transmission Control Protocol, Src Port: 3306 (3306), Dst Port: 3693 (3693),
Seq: 83, Ack: 123, Len: 11
MySQL Protocol
Packet Length: 7
Packet Number: 1
Response Code: 0
Payload: \001\017\002
In this case the ID was 15 - which is octal \017 - the middle byte in the
payload of the response to the query.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Wow! Thanks for the extensive information. I understand your
conclusion, but the rest is unfortunately just a load of jibberish (to
technical) to me ... :(

Frizzle.

Jul 21 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.