简体   繁体   English

通过PHP将数据从多语言HTML表单插入MySQL表

[英]Inserting data to MySQL table from multi-language HTML form via PHP

I have HTML form as below: 我有如下的HTML表单:

样本表格

For now there are 3 languages in future will be more, fields with prefix _lang are generating dynamically. 目前,将来会有3种语言,前缀_lang字段将动态生成。

These are input fields ( name_en , name_ru , etc. are placeholders). 这些是输入字段( name_enname_ru等是占位符)。

MySQL tables looks like: MySQL表看起来像:

Parent table Parent

Id    UserId

Details table Details

Id    ParentId    Name     Description    Language

I would like to insert data to Details like this: 我想这样向Details插入数据:

Id    ParentId    Name     Description    Language
1     1           FooEn    FooBarEn       EN
2     1           FooRu    FooBarRu       RU
3     1           FooFr    FooBarFr       FR

And to Parent table like this: 并像这样的Parent表:

Id    UserId
1     123

After form submitted I can insert successfully data to Parent table. 提交表单后,我可以将数据成功插入到Parent表中。 I've faced a problem with inserting this data to Details table. 我在将此数据插入到Details表中时遇到了问题。 I don't have idea how could I merge name_en and description_en as 1 entry. 我不知道如何将name_endescription_en合并为1个条目。

In PHP I have $language_array with all possible languages (for this example en , ru , fr ) also I'm using substr to get language code from data, but what next? 在PHP中,我具有带有所有可能语言的$language_array (对于本示例来说是enrufr ),我也在使用substr从数据中获取语言代码,但是接下来呢? Should I use foreach somehow? 我应该以某种方式使用foreach吗?

foreach($language_array as $k=>$v) {
   // there I've missed my mind
}

First time I've faced HTML form structure like this. 我第一次遇到这样的HTML表单结构。 Please, give me any suggestion. 请给我任何建议。

The simplest solution would be to retrieve the form fields from your $_POST data. 最简单的解决方案是从$_POST数据中检索表单字段。

Assuming your $_POST data has the following structure. 假设您的$_POST数据具有以下结构。

Array
(
    [name_en] => name_en
    [name_ru] => name_ru
    [name_fr] => name_fr
    [description_en] => description_en
    [description_ru] => description_ru
    [description_fr] => description_fr
)

You can then use your $language_array to iterate over and extract the associated $_POST form field values like so: $_POST['fieldname_' . $lang] 然后,您可以使用$language_array进行迭代并提取相关的$_POST表单字段值,如下所示: $_POST['fieldname_' . $lang] $_POST['fieldname_' . $lang]

$languages = ['en', 'fr', 'ru'];
$sql = 'INSERT INTO `table_name` (`Name`, `Description`, `Language`) VALUES (?, ?, ?)';
if ($stmt = mysqli_prepare($conn, $sql)) {
    mysqli_stmt_bind_param($stmt, 'sss', $name, $description, $language);
    foreach ($languages as $lang) {
       $name = array_key_exists('name_' . $lang, $_POST) ? $_POST['name_' . $lang] : null;
       $description = array_key_exists('description_' . $lang, $_POST) ? $_POST['description_' . $lang] : null;
       $language = strtoupper($lang);
       mysqli_stmt_execute($stmt);
    }
}

For details on mysqli and usage on prepared statements, please see the PHP documentation . 有关mysqli和准备好的语句的用法的详细信息,请参见PHP文档


An alternate solution would be to create a listing of form fields that you want to have displayed to the user. 一种替代解决方案是创建要显示给用户的表单域列表。

$fields = ['name' => 'Name', 'description' => 'Description'];
$languages = ['en', 'ru', 'fr'];

Then you can iterate over each field and language to render them in the browser. 然后,您可以遍历每个字段和语言以在浏览器中呈现它们。

Take note that the input field names are prefixed with their associated language as an array of fields, such as LANG[field] 请注意,输入字段名称以其相关语言作为字段数组的前缀,例如LANG[field]

foreach ($fields as $fieldName => $section) {
    echo '<fieldset>';
    echo '<legend>' . $section. '</legend>';
    foreach ($languages as $language) {
        $inputName = strtoupper($language) . '[' . $fieldName . ']';
        $placeholder = $fieldName  . '_' . $language;
        echo '<p>';
        echo '<label>';
        echo '<input type="text" name="' . $inputName . '" placeholder="' . $placeholder . '"/>';
        echo '</label>';
        echo '</p>';
    }
    echo '</fieldset>';
}

Resulting Form: 结果表格:

表格范例

Your $_POST data would then have the following structure 您的$_POST数据将具有以下结构

Array
(
    [EN] => Array
        (
            [name] => name_en
            [description] => description_en
        )

    [RU] => Array
        (
            [name] => name_ru
            [description] => description_ru
        )

    [FR] => Array
        (
            [name] => name_fr
            [description] => description_fr
        )

)

This would then allow you to insert individual entries for each language, by iterating over the $_POST form values, as an array of language associated form fields. 然后,通过迭代$_POST表单值,您可以为每种语言插入单独的条目,作为与语言相关的表单字段的数组。

$sql = 'INSERT INTO `table_name` (`Name`, `Description`, `Language`) VALUES (?, ?, ?)';
if ($stmt = mysqli_prepare($conn, $sql)) {
    mysqli_stmt_bind_param($stmt, 'sss', $name, $description, $language);
    foreach ($_POST as $language => $fields) {
       $name = array_key_exists('name', $fields) ? $fields['name'] : null;
       $description = array_key_exists('description', $fields) ? $fields['description'] : null;
       mysqli_stmt_execute($stmt);
    }
}

Disclaimer: As there was little information provided on what the parent table relates to or its association(s), it was excluded from my answer. 免责声明:由于几乎没有提供有关parent表与其关联或关联的信息,因此我的回答中未包含该信息。 I presume the OP will be able to determine how to manipulate the answer to satisfy adding the parent to the query. 我认为OP将能够确定如何操纵答案以满足将父项添加到查询中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM