无线电组和多个插入

时间:2012-12-18 02:44:58

标签: php mysql

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

<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>

每个textarea的内容($ _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'。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

您必须将广播框的值属性更改为“&#39; y&#39;到&#39; 0&#39;,&#39; 1&#39;,&#39; 2&#39;,&#39; 3&#39;。

然后您应该将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());
    }