php - 单选组和多个插入

标签 php mysql

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

<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”。我该怎么做??

最佳答案

您必须将单选框的值属性从“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());
    }

关于php - 单选组和多个插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13925546/

相关文章:

php - 通过id获取内容并通过url显示php mysql

php - MySQL 使用 Select 填充数组字符串

php - 使用 mysql_fetch_array 向前移动内部指针

mysql - Laravel 的 SQL 连接问题

php - 如何在不下载的情况下在浏览器中显示图像?

mysql - liquibase锁定异常,即使数据库已解锁

javascript - 如何在更新时显示正确的值

php - 如何在php mysql中保存多个序列化数据

php - 如何通过变量调用函数

mysql - 带有Where/Having 子句的SQL 查询案例