$_SESSION['cols1, cols2, cols3']
What's this supposed to mean? It certainly doesn't mean $_SESSION['cols1'], $_SESSION['cols2'], etc., or any combination thereof.
Oh, I see; you wanted to take those three fields and join them together with commas.
$cols = array($_SESSION['cols1'], $_SESSION['cols2'], $_SESSION['cols3']);
$strcols = join(', ', $cols);
Undefined index errors are when you try to use an array element that doesn't exist. In
$_SESSION['cols1'] .= $key . ',';
for example, if $_SESSION['cols1'] doesn't exist yet, you'll get a notice (because PHP has to use the existing value of $_SESSION['cols1'] to work out what its new value should be).
Incidentally, don't be afraid to store arrays in arrays. It can make life a lot simpler.
// Part 1
if (isset($_POST['submit'])) {
if(!isset($_SESSION['fields']))
{
$_SESSION['fields'] = array();
}
$_SESSION['fields'] = array_merge($_SESSION['fields'], $_POST);
//Got all the fields; build the list for the query.
foreach($_SESSION['fields'] as $column=>$value)
{
$_SESSION['fields'][$column] = "'".quote_smart($value)."'";
}
$colstr = join(", ", array_keys($_SESSION['fields']));
$valstr = join(", ", array_values($_SESSION['fields']));
Consult with the manual to see what those array* functions do. Play with print_r() and see what ends up where in $SESSION['fields'].