2

我有一个简单的表单,其中包含四个文本区域和一个单选组,每个文本区域旁边都有一个单选按钮。

<form action="" method="post" id="form">
<table width="35%" border="0" align="left" cellpadding="5">
  <tr>
    <td valign="top">Pathway</td>
    <td valign="top">Allowed
    <br />(One only)</td>
    <td valign="top">Comment</td>
  </tr>
  <tr>
    <td valign="top">2. No action. See again</td>
    <td valign="top"><input type="radio" name="pathway" value="y" id="pathway_1" /></td>
    <td valign="top"><textarea name="pathway_comment[]" cols="45" rows="5"></textarea></td>
  </tr>
  <tr>
    <td valign="top">2. See again</td>
    <td valign="top"><input type="radio" name="pathway" value="y" id="pathway_2" /></td>
    <td valign="top"><textarea name="pathway_comment[]" cols="45" rows="5"></textarea></td>
  </tr>
  <tr>
    <td valign="top">3. Refer to ED</td>
    <td valign="top">   <input type="radio" name="pathway" value="y" id="pathway_3" /></td>
    <td valign="top"><textarea name="pathway_comment[]" cols="45" rows="5"></textarea></td>
  </tr>
  <tr>
    <td valign="top">4. Refer to specialist</td>
    <td valign="top"><input type="radio" name="pathway" value="y" id="pathway_4" /></td>
    <td valign="top"><textarea name="pathway_comment[]" cols="45" rows="5"></textarea></td>
  </tr>
  <tr>
    <td colspan="3" valign="top"><input type="submit" name="define_pathways" id="submit" value="Add Pathways" /></td>
    </tr>
</table>
</form>

每个文本区域 ($_POST['pathway_comment']) 的内容通过循环作为单独的记录插入到 MySQL 表中:

$pathway = $_POST['pathway'];
$pathway_comment = $_POST['pathway_comment'];
if(isset($_POST['define_pathways'])){ 
    for($i=0, $count = count($pathway_comment);$i<$count;$i++) {
        $comment = $pathway_comment[$i];
        $query_level_1 = "INSERT INTO pathway (pathway_pk,case_fk,level,pathway_allowed,comment)
          VALUES ('','$vcase','1','$pathway','$comment')";
        $result_level_1 = mysql_query($query_level_1, $connection) or die(mysql_error());
    }

表结构:

`pathway_pk` int(4) NOT NULL AUTO_INCREMENT,
  `case_fk` int(3) NOT NULL,
  `level` int(1) NOT NULL,
  `pathway_allowed` char(1) NOT NULL DEFAULT 'n',
  `comment` text NOT NULL,

现在在插入时,我想将pathway_allowed四个记录之一设置为“y”,而将其他记录保留为默认的“n”。我怎样才能做到这一点??

4

1 回答 1

2

您必须将单选框的值属性从“y”更改为“0”、“1”、“2”、“3”。

然后您应该将 PHP 代码更改为:

$pathway = intval($_POST['pathway']);

$pathway_comment = array();
foreach($_POST['pathway_comment'] as $comment) {
    $pathway_comment []= mysql_real_escape_string($comment);
}
if(isset($_POST['define_pathways'])){ 
for($i=0, $count = count($pathway_comment);$i<$count;$i++) {
    $comment = $pathway_comment[$i];
    $query_level_1 = sprintf(
        "INSERT INTO pathway (             
           pathway_pk,
           case_fk,
           level,
           pathway_allowed,
           comment
        ) VALUES (
           '',
           '$vcase',
           '1',
           '%s',
           '$comment')", $pathway === $i ? 'y' : 'n');

       $result_level_1 = mysql_query($query_level_1, $connection) or die(mysql_error());
    }
于 2012-12-18T02:49:52.417 回答