如何使用数据库中的数据预填充Web表单

I have a form (actually its 4 different forms on 4 consecutive pages, but to the same effect) which users fill in to sign up to a website. There are many (100+) items in the form, including text boxes, ranges, but mostly checkboxes.

Once the user has signed up I want them to be able to edit their preferences via a simple 'edit' link on their custom homepage. My idea is that they will simply be shown the sign-up forms again, but this time they will be pre-populated using the data linked to their user account from my database.

Is this as simple as the 'edit' link in question containing a POST value to pass to the form pages:

http://www.mysite.com/something.php?edit=true

And then checking for this value in the php on the form page, and setting the values in each form item if edit is true?

The reason im double-checking on this is because it seems an awful lot of code to go through each item in the form and set its value to whatever is in the database and I dont want to do it just to find out later down the line there is a quicker and less programming intensive way of doing this!

In case it helps to find an alternative solution, Im using PDO, the user is identified by way of a session variable containing their (unique) email address, and the form names are identical to the database column names.

I would put the form inside a separate php file so it can be reused by different php scripts.

Inside this separate php file I would set the input values to POST variables.
I.e. (for this example we'll call this file myform.php)

<form action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES); ?>" method="POST">
    <input type="text" name="first_name" value="<?php echo @$_POST['first_name']; ?>" >
    <input type="text" name="last_name" value="<?php echo @$_POST['last_name']; ?>" >
    <input type="text" name="username" value="<?php echo @$_POST['username']; ?>" >
    <input type="submit" name="submit" value="<?php echo $submit_value; ?>" >
</form>


And then on the PHP side for the edit page have (this assumes your DB fields match your input field names)

<?php
foreach($db_row_fields as $field_key => $field_value)
{
     $_POST[$field_key] = $field_value;
}

$submit_value = "Update";

include 'myform.php';

Obviously only get the fields you need out of the DB.


For your sign up you only need to set $submit_value as you want the POST variables to be empty

$submit_value = "Sign Up";

include 'myform.php';

While you are using two different PHP scripts to handle sign up and editing also make validation and form processing code as reusable as possible.

You want to access the DB data on the form page, not previously and submit it via POST or GET.

Also, if you have this many fields, you may not want to have them all hard coded in HTML, but use a multidimensional array with all fields and setting which is used to create the form. This will also make filling the form with data already attributed to the current user a lot easier.