Add/view/edit mysql entries with the same php form
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?
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.
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.
精彩评论