Nick
Nick

Reputation: 2980

Get ID before submitting joomla form

I'm working on a form with a file upload functionality. When the form is submitted it includes a couple of fields with most importantly the ID and the filename. The thing is that I want the filename to include the ID. So when I upload the file using an ajax uploader, the filename would be something like: FILENAME_ID.txt

That ID is the ID joomla is going to insert into the database. The ID would be added to the filename inside the PHP script which is receiving the file. I could technically query the DB and check what the latest ID is, but i'm not sure if that's the right way to go.

What is the best way to go about doing this?

I'm using joomla 2.5, php 5.3 and mysql 5.1

P.S. If it's not possible, sorry for my stupid question O:)

Upvotes: 0

Views: 753

Answers (2)

Techie
Techie

Reputation: 45124

what you can and what you have to do is 1st insert the record and get the id of that particular row. To do that you can use

$new_id = $this->_db->insertId();

Then you can do the file upload. you can concatenate it to the to the filename($file['name']).

If you have any issue let me know

Upvotes: 1

danronmoon
danronmoon

Reputation: 3873

You can never (safely) anticipate the value of a primary key before inserting the row. You are going to have to insert and then update it using the newly inserted id for your filename. Use $db->insertid() to fetch the last inserted row's primary key for your connection.

Upvotes: 3

Related Questions