views:

72

answers:

2

I have a database that has two tables, one of which contains a foreign key back to the first. Similar to this:

Table1
    id
    description
    some
    other
    information

Table2
    table1ID
    Some
    other
    information

For each table1 there can be any number of entries in table2. I need to create a form that allows users to insert new items into tables 1 and 2. I've run into this issue in a number of projects. In one, it's saving role playing game stats. Table1 is the vehicles table and Table2 holds information on each of the vehicle's weapons.

I can think of two ways to handle this. One is to show all the information in one form, and use Javascript to allow users to add new sub-forms for each weapon as they need them. Then process it all at once. However, this is a little ugly and, obviously, relies on javascript. So it's not friendly to those who have javascript turned off.

The other way I can think of, would be to have multiple pages. The first page would allow the user to enter general data, then they could hit next to enter weapon data. Each time they hit next, they could enter a new weapon. Finally the fully entered vehicle would be displayed to them and they would be able to go back and edit each part of it as they so choose and, when they were ready, submit it.

On the one hand, this way requires a lot more processing than the first. It is much more involved. It would require that each step of the vehicle's creation be added to the database for temporary storage while the user worked on it. It would have to be marked as incomplete. I would have to create some sort of cron job to clean out all the incomplete vehicles.

On the other hand it uses no javascript.

Are there other ways of handling situations like this? If not, which of these two methods is considered the better practice? Is there a way to hang on to the data between form pages in the second method with out temporarily adding it to the db?

The tools I have available to me are a MySQL database, PHP, javascript, html/css (obviously), and a linux operating system.

+2  A: 

Hi Alcon,

What I would do is use javascript. From my point of view, not many people nowadays have javascript turned off.

But, if you choose not to use javascript, there is another way to avoid the extra overhead.

You could try to store the second table resultset in a session or cookie.

Let's face the facts, HTTP doesn't have great data persistence capabilities.

Good luck!

Jacob Relkin
Sessions and cookies are the way to go for semi-temporary data than can be easily re-fetched if necessary.For more sensitive data, you might want to create a DB dedicated to storing sensitive temporary data. Heck, you can even override the session_save_handler to save session data to the DB.
Jacob Relkin
I was pondering sessions or cookies, however, there is often an awful lot of data when I encounter this situation. How much over head is involved in using sessions or cookies for data persistence?
Daniel Bingham
Most stats I have seen put JavaScript available in over 95% of browsers. I usually figure that if I am going to use JavaScript to do something helpful, like add functionality, that I can just put a message in a noscript tag asking that they enable scripting for the site.
Frank DeRosa
Not much at all. Sessions are stored in memory, where the DB is a full-blown I/O op.Then again, if you're storing large amounts of data, i would actually go with the unload-reload option. It's too much data to fetch in one page, thereby slowing down the load. Break it up, and the user will thank you.
Jacob Relkin
@Frank, I absolutely agree. I even think about it this way: Force the user to use JavaScript, and all the other goodies ( Flash and the like ). It's almost 2010! There's no excuse for not having a fully compatible browser!
Jacob Relkin
@Jacob Sessions are not necessarily stored in the database, although this is a configurable option. The default is file system storage.
Justin Johnson
+1  A: 

I would just use JavaScript. Just like compiled applications, it is completely practical to require that the user's system meets minimum requirements, this includes JavaScript, Flash, etc. if necessary.

That said, if you're market is largely the paranoid and Lynx users, then you should consider an alternative.

If you need to store temporary data, I would not put it in the database. Instead, this is a great place to use sessions. If your only option is to store temporary data in a database table (sessions are unavailable in some environments), then I would suggest storing all that temporary data in a table separate from your real data, and using MySQL's memory table engine for that temporary table.

Justin Johnson