I've written a PHP form to add entries into a MySQL database that looks like the first picture here. However, I also want to use the same form to view and edit information already in the database as shown in the second and third photos. What's the simplest way to do this?
2 Answers
It's generally quite easy to use the same form. Another question is how you solve validation errors and not making the user re-fill the form - this gets solved as in the same process. You'll need some helpers for the select fields and any checkbox/radio fields. Exactly how much the helpers do is up to you, but these will get you started:
function radio($name, $value, $selected_value)
{
$checked= ($value === $selected_value) ? 'checked="checked"' : null;
return '<input type="radio" name="'.$name.'" value="'.$value.'" '.$checked.' />';
}
The checkbox helper's pretty much the exact same. Select options are pretty similar:
function select_option($text, $value, $selected_value)
{
$selected= ($value === $selected_value) ? 'selected="selected"' : null;
return '<option value="'.$value.'" '.$selected.'>'.$text.'</option';
}
Using them in the form follows something like this:
<?
if ($_POST)
{
// TODO: Clean post data here - xss, magic_quotes, stripslashes, etc...
$cleaned_post = clean($_POST);
// TODO: perform whatever validation you need
if (validate($cleaned_post))
{
// insert, update, etc...
$myDatabase->Save($cleaned_post, $id);
redirect_to('another_page.php'); // redirect if need be, or stay here
}
$data = $cleaned_post;
}
else // Not posted, pull from database
{
$data = array(); // Initialize to empty array
// Attempt to pull from database. $id comes from somewhere... $_GET?
// If $id isn't set, no worries.
if ($id)
$data = $myDatabase->Find($id);
}
// TODO: Do some form prep work on the data - escape " characters
?>
<input type="text" name="field_1" value="<? echo $data['field_1'] ?>" />
<? echo radio("radio_1", "1", $data['radio_1'] ?> Radio 1
<select name="my_select_field">
<? echo select_option("Option 1", "1", $data['my_select_field']) ?>
<? echo select_option("Option 2", "2", $data['my_select_field']) ?>
<? echo select_option("Option 3", "3", $data['my_select_field']) ?>
<? echo select_option("Option 4", "4", $data['my_select_field']) ?>
</select>
The form part can be the same - it just needs an array named $data going into it. The array can be empty, populated from the database, or from form data that was invalid.

- 4,984
- 28
- 36
Check to see if the value of the field is null in the database, if so, then don't fill in a value in the field:
<input type="text" name="title" value="<? if(!empty($row['title'])) echo $row['title']; ?>" />
For drop downs, make a table of values that would match the values in the drop down, then you can match your id if there is one and print something like this:
<select name="country">
<option value="85" <? if($row['country'] == 85) echo " selected"; ?> />
This is a rather broad answer for a rather broad question.

- 15,300
- 3
- 58
- 80
-
1is_empty is not a PHP function. I think you meant: `if (!empty($row['title'])) echo $row['title'];` [`empty()`](http://php.net/manual/function.empty.php) checks for NULL also. – Justin ᚅᚔᚈᚄᚒᚔ Jul 11 '11 at 22:22
-
Personally I'd just check for the existence of the primary key, then if it exists fill in everything else. Saves the DB some pointless busywork, and depending on your usage patterns could yield some serious savings. – Winfield Trail Jul 11 '11 at 22:24
-
@user Yea, this is what i would do. Although normally when you add something you have a different form and it wouldn't be the same form. Now you can always have the view and edit form together, which makes perfect sense. You may want to create a list of entries and you could click a link to show that entry and also edit it. Note this could be exactly the same form except with a different submit button (edit instead of add). Now it really depends how you would want to do this as well. – Matt Jul 11 '11 at 22:25
-
@justin thanks, updated answer. @kerin there are several ways to do it depending on your scenario, I was just showing examples of how to prepop fields. – citizen conn Jul 11 '11 at 22:27
-
@Kerin @Matt Yeah, that's exactly what I want to do! I'm not sure how to go about it though, so that's why I asked here. If it's easier, and it sounds like it is, I'll make a separate page for view/edit and add, but have them look almost identical for a streamlined user experience. – user81997 Jul 11 '11 at 22:35