Design ?: Fetch info->confirm->enter into database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djsh823
Forum Newbie
Posts: 5
Joined: Sun Jan 23, 2011 12:15 pm

Design ?: Fetch info->confirm->enter into database

Post by djsh823 »

I am new to PHP. I am trying to put together some code that will take some info (a book title, author from xml) and insert some of the info into a database. So far I have the code that will fetch and strip the info and display it, but I want a confirmation page before it is sent to a database. Basically I have two questions:

1) What is some crude code that will take existing data in variables, create a confirm button for the user and then insert the info into the database. (Remember, beginner here. I don't need anything really detailed, I can look up the details, I'm just having problems with this confirmation button.)

1) How many files should I have for this? When I install php code from major products I usually see dozens of .php files. How do I know when I should create another file? So in my case I have a single page that will take some html input, I have a request page that fetches the xml, parses, and then outputs the result. I assume this will also be the confirmation page asking the user if they want to insert this into the database. So I have:

askinfo.php - html for post variable
processrequest.php - fetches xml, parses xml, displays info, asks for confirm, AND inserts into database?

I guess when to create a new file isn't obvious to me. Aside from organization is there a reason why giant php projects don't just have one giant file? Is there something inherent to php which prevents this.

Thanks!
jack
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Design ?: Fetch info->confirm->enter into database

Post by Mordred »

1) One way is to generate a form with hidden fields corresponding to the data the user submitted, basically repeat the form he already submitted, but all fields of type="hidden" and value="htmlspecialchars($_POST['var'], ENT_QUOTES, 'your_charset')". This means that you'd give the data back to the user and he'll submit it a second time.

Option 2: store them on the server, in a session or a database. Session is better, because the data will self-destroy on logout. In the database you'll have to deal with garbage collecting the submitted but unconfirmed data. For simple small data option 1 is better, use option 2 when you need to upload files or huge textareas

The other 1 :) ) This is a personal decision you'll have to make. You can do all three steps in one file, by passing another hidden variable to tell you which phase in the process you're in, and your file will have a switch statement on this $phase to choose what to do.
Post Reply