0

Values are inside a multidimenssional array like:

Array ( 
    [0] => Array (
        [fname] => CollectiveAccess
        [lname] => Administrator
        [username] => administrator
        )
    [1] => Array (
        [fname] => Jorge
        [lname] => Ghelman 
        [username] => jorgeghe 
        )
    [2] => Array (
        [fname] => Sebastian
        [lname] => Scarano
        [username] => sscarano
        )
)

This is the loop I use to build the query:

foreach ($user_names as $value) {
    foreach ($value as  $value2) {
        echo $value2 . " "; //prints all values
        $query = "INSERT INTO users (username, fname, lname)
                  VALUES ('$value2', );";
    }
}

print_r($query) shows that only the last value from $value2 is passed to the query.

INSERT INTO users (username, fname, lname) VALUES ('test1', ); 

I'm sure it's a rookie mistake but can't figure it out. Thanks

2
  • Concatenate values into query string. Commented Aug 17, 2013 at 18:50
  • Surely you're using parametrized queries so this won't be a problem. Commented Aug 17, 2013 at 18:53

1 Answer 1

1

try this

foreach ($user_names as $value) {
    $query = "INSERT INTO users (username, fname, lname)
              VALUES ('{$value['username']}', '{$value['fname']}', '{$value['lname']}');";
    echo $query;
}
Sign up to request clarification or add additional context in comments.

1 Comment

It worked like a charm! Thanks!. I have some php book but none of them contains situations like this one, it's frustrating. Thanks again.

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.