Welcome Guest, Not a member yet? Register   Sign In
upload on isert and edit best practice ?
#1

[eluser]Future Webs[/eluser]
Heres the scenario ...

Its a bit long winded but hopefully you'll catch my drift

I have a view form that I use for both insert and update. The form contains a file upload field as well as a few other standard text fields that will hold stuff like description and title etc.

I have a controller that contains functions for edit and create.

When a file is uploaded i store it in a folder and with a name that is structured as follows.

/assets/upload_folder/item_id/my_file_item_id.jpg

item_id is the primary key value of the record in the database. The primary key is set to a tinyint (4) zerofill and so if we were dealing with item 56 the file and upload destination should end up as

/assets/upload_folder/0056/my_file_0056.jpg

Now if im working on an edit its not too bad as I already know the item id as its passed in the url. If on the other hand im creating a new item I dont have the item id until the insert has been done as it would need to come from the database helper function $this->db->insert_id() http://ellislab.com/codeigniter/user-gui...lpers.html or something similar

Now that brings me around to where i could do with some advice. Im sure there are many ways to go about this and Im not sure which is the best one.

Which order should I do things in ?

Quote:PLAN 1

- validate upload
- validate form
- do upload
- do insert / edit

The problem here is that i dont know the id number until ive done the insert and so cant set the folder name or file name.

Quote:PLAN 2

- validate upload
- validate form
- do insert / edit
- do upload

Problem here is that if the upload fails I have already added the info to the database and would also probably need to do another edit to save the new file name of the upload

Quote:PLAN 3

- validate form
- do insert / edit
- validate upload
- do upload

again if the upload fails i have already saved the insert data into the database.

I know its a common situation to have an upload form with a few text fields too. i guess the main difference for me is that im fussy how the folders and files are named.

so how would you all go about it ?
#2

[eluser]xwero[/eluser]
I go for 3 because the database entry is, most of the times, more important than the file itself. And if people are going to upload something they are not going to cancel it because there is something wrong moving the file to the server.

The thing i change if an upload of a new entry fails is go from new to edit status to let the people know some of the data they inputted is already saved.
#3

[eluser]kgill[/eluser]
This is precisely why transactions were invented - use plan 2, begin a transaction, if the upload fails rollback.
#4

[eluser]Future Webs[/eluser]
I had thought about transactions as I had seen a similar thing in the past using interakt extensions in dreamweaver

from the user guide

Quote:In MySQL, you'll need to be running InnoDB or BDB table types rather than the more common MyISAM. Most other database platforms support transactions natively.

I know my database is mysql MyISAM
#5

[eluser]Future Webs[/eluser]
[quote author="xwero" date="1228504106"]I go for 3 because the database entry is, most of the times, more important than the file itself. And if people are going to upload something they are not going to cancel it because there is something wrong moving the file to the server.

The thing i change if an upload of a new entry fails is go from new to edit status to let the people know some of the data they inputted is already saved.[/quote]

Say in this situation the file upload fails because for example its the wrong size or type and then the user does not bother fixing it after i send them back to the edit page. Im then stuck with a bad entry with no actual file
#6

[eluser]kgill[/eluser]
Well if your version of MySQL is 5.x, you should have the option of creating your table as InnoDB, assuming there's nothing in the table yet, is there a reason you couldn't drop it and recreate it as InnoDB?

If that's not possible what about faking a sequence? Create a table with 2 columns: an auto_increment field and whatever, insert into that table to get your number. That way you can go with plan 1, upload first then insert - you just need to drop the auto_increment on the uploads table.
#7

[eluser]xwero[/eluser]
[quote author="w3bm" date="1228505689"]Say in this situation the file upload fails because for example its the wrong size or type and then the user does not bother fixing it after i send them back to the edit page. Im then stuck with a bad entry with no actual file[/quote]

If they don't bother to add the file. They see an item is added but it's not complete. Not complete items just don't appear in the frontend.
#8

[eluser]Future Webs[/eluser]
[quote author="xwero" date="1228507684"][quote author="w3bm" date="1228505689"]Say in this situation the file upload fails because for example its the wrong size or type and then the user does not bother fixing it after i send them back to the edit page. Im then stuck with a bad entry with no actual file[/quote]

If they don't bother to add the file. They see an item is added but it's not complete. Not complete items just don't appear in the frontend.[/quote]

feels like a messy way to do it. at the moment Im leaning towards the rollback idea. I could adjust the table type. Does anyone know the advantages of using InnoDB rather then MyISAM

Thanks for your suggestions so far guys. Im still open to hear how anyone else deals with this type of thing




Theme © iAndrew 2016 - Forum software by © MyBB