I have HTML form as below:
For now there are 3 languages in future will be more, fields with prefix _lang
are generating dynamically.
These are input fields ( name_en
, name_ru
, etc. are placeholders).
MySQL tables looks like:
Parent
table
Id UserId
Details
table
Id ParentId Name Description Language
I would like to insert data to Details
like this:
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:
Id UserId
1 123
After form submitted I can insert successfully data to Parent
table. I've faced a problem with inserting this data to Details
table. I don't have idea how could I merge name_en
and description_en
as 1 entry.
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? Should I use foreach
somehow?
foreach($language_array as $k=>$v) {
// there I've missed my mind
}
First time I've faced HTML form structure like this. Please, give me any suggestion.
The simplest solution would be to retrieve the form fields from your $_POST
data.
Assuming your $_POST
data has the following structure.
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]
$_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 .
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]
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
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.
$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. I presume the OP will be able to determine how to manipulate the answer to satisfy adding the parent to the query.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.