Using a loop to go do things with form inputs...

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
SunR2
Forum Newbie
Posts: 3
Joined: Tue Dec 21, 2004 5:43 pm

Using a loop to go do things with form inputs...

Post by SunR2 »

Hi,

This is a kind of odd question, as I may not be able to explain properly what I'm trying to do... I'm a relative newbie at PHP, though I've built a couple of websites with it and MySQL.

I'm in the process of building a series of forms for an application. Each form is on a seperate page. The first form (personaldetails.php) would pass the data to the second form (form_a.php). In the head of this page would be code to do something with the data from the previous page - in this case, take the variables and write them to a database; the rest of form_a.php would then display its questions. Continuing on, form_a.php would pass its data on to form_b for processing, before form_b would display it's form. And so on, up to form_e.php (which would have a processing page after it called "final.php" or somesuch).

The main reason for seperating the fields out into so many forms is that there are a lot of variables - in fact, 70 or so of them. Don't ask! 8O

Anyhow, the usual way of taking these values (assuming globals is off) would be to look in the $_POST array, and then have a line for each which would do something like:

Code: Select all

$name = $_POST['name'];
Once one has all the information in regular variables, it'd be a case of then constructing a SQL "INSERT INTO" string, using the variables in the appropriate places, and then running that string on the database. All well and good... but...

There are going to be six places where this happens (one on each of form_a -> form_e plus the final page), each of which would have anywhere between 3 and 30-odd variables... I really do not fancy doing this all by hand, as it would open the door to a ton of problems from forgetting to change even one bit of syntax after copying and pasting... plus it would be a heck of a lot of work.

I got thinking:

The $_POST data is already an array. Loops work nicely with arrays...

What I'd like to do would be to have a small bit of re-usable code which I could stick in at the top of any page which needed it, which looped through every $_POST element, and created enough variables on the fly to store this information. Then a little bit of code which would look at those variables which have been created on the fly, and construct an SQL "INSERT INTO" string as necessary, as short or long as it needs to be depending on the number of variables referenced in it.

Just a point also, the input names which are stored in the $_POST array will correspond precisely to the fields in the database.

Thinking about it, it'd need 2 loops - one to go through $_POST to create x-many variables (or an array of x-many elements...) and the other to then go through those variables (or array elements) and concatenate an SQL string from it...

The obvious advantages of this would be one piece of code which is re-usable between pages - and, in fact, not just between pages but on any form-based thing which does a similar job no matter how many or few form elements there are.

I know the explanation is pretty poor, but does anyone see what I'm trying to get at? Is what I'm wanting feasible? In effect I'd be creating a number of temporary variables (unknown at coding time) on the fly, naming them based on their name in the $_POST array, and then using that unknown number of variables to construct an SQL query.

A simple working example would be great, if anyone has faced the same quandry before... alternatively pointing me in the right direction would be great!

Thanks in advance,

Sunil
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

<?php
$fields = '';
foreach($_POST as $field => $data) {
    if(strlen($fields))
        $fields .= ',\n';
    $fields .= "\t`" . mysql_real_escape_string($field) ."`='" . mysql_real_escape_string($data) . "'";
}
$sql = '
    INSERT INTO
        tablename
    SET
' . $fields;
?>
Post Reply