I don't know why you want to insert using SELECT
, as this construct is to insert from already existing table data. You can, however, insert multiple VALUES
with a single statement. You'd need to do :
// you should filter out values from your $_POST...
$ignoredFields = array('submit', ...);
$fields = array_intersect_key($_POST, array_flip($ignoredFields));
$values = array();
foreach ($fields as $key => $value) {
$key = mysql_real_escape_string($key);
$value = mysql_real_escape_string($value);
$values[] = "'{$key}', '{$value}'";
}
// creation the insert string
$query = 'INSERT INTO `tabke` (`key`,`value`) VALUES ('.implode(,'),(', $values).')';
$result = mysql_query($query);
** Note ** : I suppose that your table tabke
look something like
+---------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| key | varchar(64) | NO | | NULL | |
| value | text | NO | | NULL | |
+---------+------------------+------+-----+---------+----------------+
As long as your query does not extend max_allowed_packet, this will work just fine. In case your data exceed that size, you can simply use array_chunk
and iterate through the chunks and building the INSERT
with each chunks.