1

hello i have the following javascript which populates my fields and works fine

<script type="text/javascript">
var count = 0;
$(function(){
$('p#add_field2').click(function(){
    count += 1;
    $('#container').append(
            '<strong>Link #' + count + '</strong><br />'
            + '<input id="field_' + count + '" name="fields[]' + '" type="text" /><input     id="field1_' + count + '" name="idrima' + '" type="text" /><input id="field2_' + count + '" name="code_title' + '" type="text" /><input id="field3_' + count + '" name="name_title' + '" type="text" /><input id="field4_' + count + '" name="vathmos_title_a' + '" type="text" /><input id="field5_' + count + '" name="vathmos_title_d' + '" type="text" /><input id="field6_' + count + '" name="etos' + '" type="text" /><br />');

});
});
</script>

my problem is that when i use foreach i m able to add just the last field poppulation and not all. So i think foreach doesnt work cause adds only the last record. My code is

if ($_POST['fields']) {

    //get last inserted userid
    $inserted_user_id = $db->last_insert_id();

    //Loop through added fields
    foreach ( $_POST['fields'] as $key=>$value ) {

        //Insert into websites table
        $sql_website = sprintf("INSERT INTO spoudes (idrima,code_title) VALUES ('%s','%s')",
                    mysql_real_escape_string($_POST['idrima']),
                    mysql_real_escape_string($_POST['code_title']));
        $result_website = $db->query($sql_website);
        $inserted_website_id = $db->last_insert_id();


        //Insert into users_websites_link table
        $sql_users_website = sprintf("INSERT INTO spoudes_link (id_fakelos, id_spoudes) VALUES ('%s','%s')",
                               mysql_real_escape_string($inserted_user_id),
                               mysql_real_escape_string($inserted_website_id) );
        $result_users_website = $db->query($sql_users_website);


    }

1 Answer 1

1

You should use <input name="idrima[]"> and <input name="code_title"> as each time you add a new field, it is overwriting that field on the post.

foreach ( $_POST['fields'] as $key=>$value ) {

    //Insert into websites table
    $sql_website = sprintf("INSERT INTO spoudes (idrima,code_title) VALUES ('%s','%s')",
                mysql_real_escape_string($_POST['idrima'][$key]),
                mysql_real_escape_string($_POST['code_title'][$key]));
    $result_website = $db->query($sql_website);
    $inserted_website_id = $db->last_insert_id();


    //Insert into users_websites_link table
    $sql_users_website = sprintf("INSERT INTO spoudes_link (id_fakelos, id_spoudes) VALUES ('%s','%s')",
                           mysql_real_escape_string($inserted_user_id),
                           mysql_real_escape_string($inserted_website_id) );
    $result_users_website = $db->query($sql_users_website);


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

2 Comments

it returns me error Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in
that is now a array and you'll have to access it via index as well within the foreach loop. try something like $_POST['idrima'][$key], $_POST['code_title'][$key]

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.