将多个表单选择插入一个单表列(集)

I have a simple form with the following:

<form action="#" method="post" name="form1" >
    <input name="criteria1" size="64">
    <input name="criteria2" size="64">
    <select name="criteria3[]"multiple="multiple" >
        <option value="5 ">5</option>
        <option value="7">7</option>
        <option value="10">10</option>
    </select>
</form>

Now, if the user selects more then one option from criteria3, how do I insert that in a single database table field as well as insert the other two criteria in their own column?

I have tried many different ways and just get no where. My last attempt was as follows:

$values = array();

$type = explode(",", $_POST['criteria3']);

foreach ($type as $value) {
    $values[] = sprintf('(%d)', $value);
}

$values = implode(',', $values);

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
   $insertSQL = sprintf("INSERT INTO table (name, date, number) 
                         VALUES (%s, %s, '$values')",
       GetSQLValueString($_POST['criteria1'], "text"),
       GetSQLValueString($_POST['criteria2'], "text"));

Not really sure what is going wrong – have tried several iterations and am getting no where.

Here are the most recent errors:

Warning: explode() expects parameter 2 to be string, array given in /filename.php on line 84

Warning: Invalid argument supplied for foreach() in /filename.php on line 86

Explode only works with a string. What you get from your form for criteria3 is an array, so you should act accordingly:

$criteria1 = $_POST['criteria1'];
$criteria2 = $_POST['criteria2'];
$criteria3 = implode(",", $_POST['criteria3']);

Here we generate a string based on the content of the table returned by your form. We seperate each element by a comma.

Then we create the SQL query.

$sql = sprintf("INSERT INTO table (name, date, number) VALUES (%s, %s, %s);", $criteria1, $criteria2, $criteria3);

Hope this helps.

remove the explode function from $_POST['criteria3'], Because it is already array it not need of explode so you try directly

       $type = $_POST['criteria3'];

        foreach($type  as $k => $v) {
           // here do the INSERT query with value $v
      }

or if you prefer this link
1).Getting data from a multiple select dropdown with PHP to insert into MySQL

hope this helpful for you :)

          <form action="" method="post" name="form1" >
<input name="criteria1" size="64">
<input name="criteria2" size="64">
<select name="criteria3[]" multiple >
<option value="5 ">5</option>
<option value="7">7</option>
<option value="10">10</option>
</select>

       <?php
   if(isset($_POST['submit']))
  {
    $criteria1 = $_POST['criteria1'];
    $criteria2 = $_POST['criteria2'];
    if(isset($_POST['criteria3']))
     {
       $query = "insert into table_name (criteria1, criteria2, criteria3) values";
        foreach($_POST['criteria3'] as $list)
         {
          $query .= "('$criteria1','$criteria2','$list')";
          }
         echo $query;                       
     }
  }
  ?>

Try the following(If you want separate records corresponding to separate values for criteria3 for a single entity):

$criteria1 = $_POST['criteria1'];
$criteria2 = $_POST['criteria2'];
$arr=$_POST['criteria3'];
foreach($arr as $criteria3)
{
    $sql="insert into table_name (criteria1, criteria2, criteria3) values('".$criteria1."', '".$criteria2."', '".$criteria3."')";
    mysql_query($sql);
}

And if you want a single record for a single entity with multiple criteria3-values use the following code:

$criteria1 = $_POST['criteria1'];
$criteria2 = $_POST['criteria2'];
if(isset($_POST['criteria3'])) //true when at least on of the options is selected.
{
    $criteria3=implode(',', $_POST['criteria3']);
}
else //i.e. when no option is selected.
{
    $criteria3='';
}
$sql="insert into table_name (criteria1, criteria2, criteria3) values('".$criteria1."', '".$criteria2."', '".$criteria3."')";
mysql_query($sql);

Since I am the only one that will be inserting data - I found a crude work around until I can get the code that Rajesh provided to work - I think this is the right path if I can get it to work.Thanks to all for your input. If I could, I would give you all the up vote!