0

I am new to PHP, so I am looking for some input on how to make my project a little simpler.

I have a form in which a user can create a list of song, the Submit button then sends it to an intermediate page that saves it to a MySQL database for later use, the intermediate page then forwards them on to the final page that shows them the lyrics of the songs in the order that they chose them.

Originally I had the intermediate page and the final page combined, but ever time a user refreshed the page it would resubmit the data to the DB. What I have works, but it seems like there should be an easier way to accomplish this.

KJYe.Name
  • 16,969
  • 5
  • 48
  • 63
micahmills
  • 868
  • 3
  • 14
  • 22
  • 1
    slightly off-topic, but if you are building your own stuff in PHP from the ground up, you could be better off by using an existing framework like Symfony, solarphp or CakePHP. You will avoid reinventing the wheel, and most of the beginning issues like this one are already implemented. – kikito Dec 24 '10 at 17:38
  • Thanks egarcia I will look into the frameworks you mentioned. I use jquery framework when doing javascript stuff, but hadn't really thought about it for PHP. – micahmills Dec 24 '10 at 18:35

2 Answers2

0

Best practice is to redirect after database operation to success / failure page.

You can have form & intermediate combined and a final success page, on failure you need to return back form.

Ish
  • 28,486
  • 11
  • 60
  • 77
0

@micahmills: An easier way of stopping duplicate data from being added to the database? Well, it would depend on what you'd consider "easier" -- less code? Less steps? Something else?

What you could do is generate a unique hash or token that submits with the form. This token is then stored in a session after successfully inserting to the database. Attempts to repost the form will then fail because the token sent with the form will be the same as the one stored in the session.

Redirecting to another page after posting to the database is still one of the best and simplest ways of preventing duplicate data being posted though.

stealthyninja
  • 10,343
  • 11
  • 51
  • 59