I'm new to PHP and I'm trying to insert data from a nested array into a database. I'm noticing though that only the last key:value gets put in (seems almost as if everything else before gets overwritten). How do I get a new line of each data so everything gets put in?
$tmpArray = array(array("one" => abc, "two" => def), array("one" => ghi, "two" => jkl));
// Scan through outer loop
foreach ($tmpArray as $innerArray) {
// Scan through inner loop
foreach ($innerArray as $key => $value) {
if ($key === 'one') {
$sql = "INSERT INTO test2 (alpha, beta) VALUES ('$key', '$value')";
}
}
}
For simplicty, all I'm trying to do is to get "one" and "abc" put into alpha and beta. Then have another row of the table input "one" and "ghi". But when I run the code, all I get is "one" and "ghi". When I put an echo statement though, all the correct stuff gets printed. Just don't understand why they aren't getting input into my tables.
What am I doing wrong? Thanks!
$sqldoes get overwritten each time through the loop.