0

I have this:

<input type="text" name="menu_link_text*1" value="italian">
<input type="text" name="menu_link_text*2" value="english">

where 1 and 2 identify the language in my database (is lang_id)

How as I do with php to save each menu_link_text and its lang_id in this table structure:

tb_menu_link:

menu_link_id
menu_link_text
lang_id

this is my function for the update how do I change it?

$db->update(
'tb_menu_link_content',
array(
    'menu_link_text'=>$check->sanitize($_POST['menu_link_text']),
),
'menu_link_id = "'.$_POST['menu_link_id'].'" AND lang_id = "'.$_POST['lang_id'].'"');
3
  • Why your are not using array name="menu_link_text[]"? Commented Oct 8, 2013 at 11:42
  • You should use arrays Commented Oct 8, 2013 at 11:42
  • 1. Enclose your input elements in a form and specify action for the form. 2. get the values posted from the form in the page which is set as the action of your form. 3. Insert into the database. Commented Oct 8, 2013 at 11:43

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply! I edited my question by inserting the function to update the table. I do not understand how to save a new line if it does not exist or save in the same row if there menu_link_id, lang_id
I updated my post with a small tutorial/how I should do it in the main lines
0

I am just briefing Hendriq's answer,

Convert input to

<input type="text" name="menu_link_text[1]" value="italian">
<input type="text" name="menu_link_text[2]" value="english">

Now in php file do follwing changes,

$menu_link_text=$_POST['menu_link_text'];
echo $menu_link_text[1]; //it will print "italian"
echo $menu_link_text[2]; // it will print "english"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.