this is my first question here so please be nice :)
I'm trying to insert multiple records, but I keep getting a mysql error, #1136 Column count doesn't match value count at row 1. I've followed the guide here: http://docs.joomla.org/Inserting,_Updating_and_Removing_data_using_JDatabase
Part of my issue is that the columns won't necessarily always be the same in the array. For example, 'address1' is required, but 'address2' and 'address3' are not. All available fields exist in the db table though, so I just need to specify the columns as keys for each value set.
I feel like I'm missing something quite obvious, but having spent far too many hours at this, I hope someone will be able to easily point it out.
Here's my error:
1136 Column count doesn't match value count at row 1 SQL=INSERT INTO jos_supersite_contact
Below is the code I'm using....
foreach ($data[0]['result'] as $results) {
$db = JFactory::getDbo();
$db->getQuery(true);
foreach ($results as $key => $value) {
$columns[] = $db->quoteName(str_replace('.', '_', $key));
$rows[] = $db->quote($value);
}
$query
->columns($columns)
->values(implode(',',$rows))
->insert($db->quoteName('#__supersite_contact'));
$db->setQuery($query);
if (!$db->execute()) {
throw new Exception($db->getErrorMsg());
}
}