I need to put alot of radio button values into the database, so I post 300 values to a processing page, where I need to sort things out a bit.
I want to be able to differentiate between each radio buttons value and name (when posted) so I can insert them into the database. This is my code: (but maybe i need a jagged array?)
VALUE:
foreach ($_POST as $key => $value)
{
$value = $value.',';
}
POST NAME: ???
foreach ($_POST[name]?? as ??? => $postname)
{
$postname = $postname.',';
}
Then I need to differentiate between the value and name and put in the correct columns in the database:
mysql_query("INSERT INTO tabke SELECT '$longstring' or die(mysql_error());;
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.
It's going to be $key, but there will only be one post per distinct radio "Group", also you should be sanitizing your inputs for SQL injection.