-1

我一直在尝试创建一个combobox具有两个输入字段的,本质上是一个表单。

这是我的组合框:

<select name="answers[]" id="answers">
<option value="<?PHP echo $cevap_list['CRM7101']; ?>,<?PHP echo $cevap_list['CRM7105']; ?>"><?PHP echo $cevap_list['CRM7103']; ?></option>

我试图在 PHP 脚本中以 POST 形式接收这些值:

$answers= $_POST['answers']; 

for($p=0;$p<=count($answers);$p++){ 
    $answer_explode = explode(",",$answers[$p]);  
    $answer_id = $answer_explode[0];  
    $answer_value = $answer_explode[1]; 
} 

for($i=0;$i<count($answer_id);$i++){ 
$cevapkontrol = trim($answer_id[$i]); 

 $sorgu = mysql_query("insert into CRM9001(CRM9107,CRM9106) values ('".$answer_value[$i]."','".$answer_id[$i]."') "); 
}  

但它似乎不起作用?难道我做错了什么?

4

1 回答 1

0
  1. 使用foreach()构造
  2. 您在每次迭代中都覆盖了先前的 id 和 value
<?php

if ($_POST)
{
    $results = array();
    foreach ($_POST['answers'] as $answer)
    {
        $answer_explode = explode(",", $answer);
        $results[] = array(
            'value' => trim($answer_explode[1]),
            'id' => trim($answer_explode[0])
        );
    }

    foreach($results as $result)
    {
        mysql_query("insert into CRM9001(CRM9107,CRM9106) values ('".$result['value']."','".$result['id']."') ");
    }
}
<select name="answers[]">
    <option value="some_id,some_value">some_name</option>
</select>
<select name="answers[]">
    <option value="some_id2,some_value2">some_name2</option>
</select> <input type="submit">
于 2012-12-13T10:56:21.577 回答