使用相同的 php 表单添加/查看/编辑 mysql 条目

发布于 2024-11-19 16:15:45 字数 170 浏览 1 评论 0原文

我编写了一个 PHP 表单来将条目添加到 MySQL 数据库中,该数据库看起来像这里的第一张图片 。但是,我还想使用相同的表单来查看和编辑数据库中已有的信息,如第二张和第三张照片所示。最简单的方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掌心的温暖 2024-11-26 16:15:45

使用相同的形式通常很容易。另一个问题是如何解决验证错误而不是让用户重新填写表单 - 这可以在同一过程中解决。您将需要一些用于选择字段和任何复选框/单选字段的帮助程序。帮助程序的确切工作量取决于您,但这些将帮助您入门:

function radio($name, $value, $selected_value)
{
    $checked= ($value === $selected_value) ? 'checked="checked"' : null;

    return '<input type="radio" name="'.$name.'" value="'.$value.'" '.$checked.' />';
}

复选框帮助程序几乎完全相同。选择选项非常相似:

function select_option($text, $value, $selected_value)
{
    $selected= ($value === $selected_value) ? 'selected="selected"' : null;
    return '<option value="'.$value.'" '.$selected.'>'.$text.'</option';
}

在表单中使用它们如下所示:

<?
    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>

表单部分可以是相同的 - 它只需要一个名为 $data 的数组进入其中。该数组可以为空,可以从数据库填充,也可以从无效的表单数据填充。

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.

天生の放荡 2024-11-26 16:15:45

检查数据库中该字段的值是否为空,如果是,则不要在该字段中填写值:

<input type="text" name="title" value="<? if(!empty($row['title'])) echo $row['title']; ?>" />

对于下拉列表,创建一个与下拉列表中的值匹配的值表,然后如果有的话,您可以匹配您的 ID 并打印如下内容:

<select name="country">
<option value="85" <? if($row['country'] == 85) echo " selected"; ?> />

This is a相当广泛的答案,针对一个相当广泛的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文