0

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());;  
2
  • 1
    what is your table tabke structure? Commented Jul 18, 2011 at 17:45
  • jagged array? What is that? Commented Jul 18, 2011 at 18:17

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

any field that you need to ignore from your form. For example, if you have a reset button (name="reset") and a submit button (name="submit"), you would have $ignoredFields = array('reset', 'submit');. For example.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.