Convert the input to
<input type="text" name="menu_link_text[1]" value="italian">
<input type="text" name="menu_link_text[2]" value="english">
Then $_POST will container (or get if you use that, but you should use POST for ~99% of the cases)
array(
"menu_link_text" => array(
1 => "italian",
2 => "english",
)
)
Now you can easily get the language id and match it with you database
I assume you, know how a db works + post
@edit after comment, a made it into a huge post but I hope it helps
I dont know what your db scheme is so I assume the following:
menu_link_id INT
lang_id INT
text VARCHAR
In your post you send the menu_link_id (can be empty) and input (as given in answer)
A simple post would then look like:
array(
"menu_link_id" => "1", // can also be "", so a new menu link is made
"menu_link_text" => array(
1 => "italian",
2 => "english",
)
)
First you should check if the values exists, and are valid values
menu_link_id is valid when it is nummeric or the string length is 0
menu_link_text is valid when it is an array
if (
// simple check if the values exists
(isset($_POST['menu_link_id'] && isset($_POST['menu_link_text'])
// check if the id is valid
&& (is_numeric($_POST['menu_link_id']) || strlen($_POST['menu_link_id'] == 0)
// check if the text is valid
&& (is_array($_POST['menu_link_text']))
)
Well know we know we have "valid" data, we should start saving (update or insert it) (I am no MySQL guy but know you can do it in one statement (update and insert) but I don't know how)
Now we should start saving it. To start we make an array of used lang_ids. This will be used to match against if it is new or already exists. I don't know how which or what db you use, so I assume MySQl, and then PDO. Also I assume in the whole project the value $db is equal with a pdo connection
$lank_ids = array();
if (strlen($_POST['menu_link_id'] > 0) {
$action = $db->prepare('SELECT lang_id FROM menu_items WHERE menu_link_id = :id');
// I already validated the value, but be defensive and be always save (so always use protection)
$action->execute(array(':id' => $_POST['menu_link_id']));
$lank_ids = $action->fetchColumn();
}
Now we shall insert those dam values
$valid_lang_ids = array(1, 2);
foreach($_POST['menu_link_text'] as $lang_id => $text) {
// Check the lang_ids are valid, I use now a hardcoded array
if(in_array($lang_id, $valid_lang_ids)) {
if (in_array($lang_id, $lank_ids )) {
// The lang_id is already know so update
$action = $db->prepare('UPDATE menu_items SET text = :text WHERE menu_item_id = :id AND lang_id = :lang_id');
$action->execute(array(
':id' => $_POST['menu_link_id'],
':lang_id' => $lang_id,
':text' => $text
);
} else {
// The lang_id is not set, so insert
$action = $db->prepare('INSERT INTO menu_items (menu_link_id, lang_id, text) VALUES (:id, :lang_id, :text)';
$action->execute(array(
':id' => $_POST['menu_link_id'],
':lang_id' => $lang_id,
':text' => $text
);
}
}
}
Warning
Almost all the code is out of the dead so I assume it has some bugs but the main line should be clear
I did not sanatize the text, so XSS is possible!!!
name="menu_link_text[]"?