473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

acquiring value of primary key from uploaded file

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
10 2801

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
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
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

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
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
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

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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
0
by: cschang | last post by:
I have a file upload module written in .aspx. I want to write the filename (just the name) uploaded back to the my parent .asp file (the ..aspx file was a pop-up window related to the .asp) so...
2
by: Peter Row | last post by:
Hi, To allow users to upload files larger than 4MB in size I added: <httpRuntime maxRequestLength="102400" executionTimeout="600" /> ...to my web.config file. Fair enough this works fine....
4
by: Dotcom | last post by:
I have an ASP.NET application that is mysteriously acquiring height and width attributes on a particular IMG element on multiple pages. This is not being caused by someone editing or uploading new...
2
by: Brandon | last post by:
I created a setup project in VS.NET 2k5 and added an additional dialog to ask the user where they would like to place icons for my application. The options are Desktop and QuickLaunch Menu. When...
3
bhcob1
by: bhcob1 | last post by:
Hi guys, This is the situation. I have 3 tables, with the following relevent fields tblSubFile (Substantiation Files) - autonumber - Primary Key - Primary Key
66
by: happyse27 | last post by:
Hi All, my html code is sno 1) and perl code is sno 2). a) I tried to print $filename and it cant print out the value, only blank was displayed, and the file could not be uploaded. And it...
8
by: ajeeshc | last post by:
i want to display the path of file in browser that i am uploaded to the server for later downloading but when iam trying to do this using the mysql_fetch_array() it not working an error like ...
4
by: liberty1 | last post by:
Hi everyone. I appreciate your effort at helping newbies like me. I have the following problems and will appreciate urgent help. PROBLEM NUMBER 1: Using PHP and MySQL, I am able to upload...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.