繁体   English   中英

从for循环插入-将选择选项值仅添加到一条记录

[英]Insert from for loop - add select option value to only one record

我在for循环中发生插入,将4条记录插入数据库。 我只想让这些新记录中的一个从表单的选择菜单中获取值,该值包含在变量$ action中。 该菜单仅针对表单上的一条记录出现,具体取决于用户的选择。

表结构为:

CREATE TABLE IF NOT EXISTS `pathway` (
  `pathway_pk` int(3) NOT NULL AUTO_INCREMENT,
  `case_fk` int(3) NOT NULL,
  `level` int(1) NOT NULL,
  `pathway_allowed` char(1) NOT NULL DEFAULT 'n',
  `pathway_action_fk` int(2) NOT NULL DEFAULT '0',
  `comment` text NOT NULL,
  `created` datetime NOT NULL,
  `created_by` int(3) NOT NULL,
  `updated` datetime NOT NULL,
  `updated_by` int(3) NOT NULL,
  PRIMARY KEY (`pathway_pk`)
)

PHP:

$author_pk = $_GET['author_pk'];
$case_pk = $_GET['case_pk'];
$patient_pk = $_GET['patient_pk'];

$pathway_allowed = intval($_POST['allowed']);

$action = mysql_real_escape_string($_POST['actions']);


if(isset($_POST['submit'])){ 

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

       $result = mysql_query($query, $connection) or die(mysql_error());

其余的部分:

<script type="text/javascript">
$(document).ready(function() {
$("#jMenu").jMenu();
$("select, input, a.button, button").uniform();


$("input:radio[id='allowed_0']").click(function() { 

$("#required_p2").remove();
$("#required_p3").remove();
$("#required_p4").remove();

if($(".p1_action").text().trim()==""){
$(".p1_action").append('<div class="required" id="required_p1"><select name="actions" id="actions"><option>Select an action for this pathway...</option></select></div>');
}

$.getJSON('../scripts/get_pathway_actions.php', function(data){
    var html = '';
    var len = data.length;
    for (var i = 0; i< len; i++) {
        html += '<option value="' + data[i].pathway_action_pk + '">' + data[i].action + '</option>';
    }
    if($('select#actions option').length == 1){
    $('#actions').append(html);
    }
});
});


$("input:radio[id='allowed_1']").click(function() { 

$("#required_p1").remove();
$("#required_p3").remove();
$("#required_p4").remove();

if($(".p2_action").text().trim()==""){
$(".p2_action").append('<div class="required" id="required_p2"><select name="actions" id="actions"><option>Select an action for this pathway...</option></select></div>');
}

$.getJSON('../scripts/get_pathway_actions.php', function(data){
    var html = '';
    var len = data.length;
    for (var i = 0; i< len; i++) {
        html += '<option value="' + data[i].pathway_action_pk + '">' + data[i].action + '</option>';
    }
    if($('select#actions option').length == 1){
    $('#actions').append(html);
    }
});
});



$("input:radio[id='allowed_2']").click(function() { 

$("#required_p1").remove();
$("#required_p2").remove();
$("#required_p4").remove();

if($(".p3_action").text().trim()==""){
$(".p3_action").append('<div class="required" id="required_p3"><select name="actions" id="actions"><option>Select an action for this pathway...</option></select></div>');
}

$.getJSON('../scripts/get_pathway_actions.php', function(data){
    var html = '';
    var len = data.length;
    for (var i = 0; i< len; i++) {
        html += '<option value="' + data[i].pathway_action_pk + '">' + data[i].action + '</option>';
    }
    if($('select#actions option').length == 1){
    $('#actions').append(html);
    }
});
});



$("input:radio[id='allowed_3']").click(function() { 

$("#required_p1").remove();
$("#required_p2").remove();
$("#required_p3").remove();

if($(".p4_action").text().trim()==""){
$(".p4_action").append('<div class="required" id="required_p4"><select name="actions" id="actions"><option>Select an action for this pathway...</option></select></div>');
}

$.getJSON('../scripts/get_pathway_actions.php', function(data){
    var html = '';
    var len = data.length;
    for (var i = 0; i< len; i++) {
        html += '<option value="' + data[i].pathway_action_pk + '">' + data[i].action + '</option>';
    }
    if($('select#actions option').length == 1){
    $('#actions').append(html);
    }
});
});


});

function validate(){
valid = true;

if ( document.form.comment_1.value == ""){
    alert ( "Please enter a comment for Pathway 1" );
    valid = false;
    return valid;
}

if ( document.form.comment_2.value == ""){
    alert ( "Please enter a comment for Pathway 2" );
    valid = false;
    return valid;
}

if ( document.form.comment_3.value == ""){
    alert ( "Please enter a comment for Pathway 3" );
    valid = false;
    return valid;
}

if ( document.form.comment_4.value == ""){
    alert ( "Please enter a comment for Pathway 4" );
    valid = false;
    return valid;
}




return valid;
}

function delayer(){
window.location = "create_pathways.php?author_pk=<?php echo $author_pk; ?>&case_pk=<?php echo $case_pk; ?>&patient_pk=<?php echo $patient_pk; ?>"
}
</script>
<body>
<?php include('menu_bar.php'); ?>
<div class="settings">
  <div class="buttonn"> <a href="<?php echo $_SERVER['PHP_SELF'], '?logout=true' ?>" class="buttonn">Logout</a> </div>
  <div class="buttonn"> <a href="change_password.php?author_pk=<?php echo $author_pk; ?>" class="buttonn">Change Password</a> </div>
</div>
<div class="title">Create Pathways for Level 1<span class="message"><?php echo $message; ?></span></div>
<div class="top_form">
  <form action="" method="post" name="form" id="form" onSubmit="return validate();">
    <table width="182%" border="0" cellpadding="5">

      <tr>
        <td valign="top"><strong>Pathway</strong></td>
        <td align="center" valign="top"><strong>Allowed</strong></td>
        <td colspan="2"><strong>Blocking or Accepting Comment</strong></td>
        <td valign="top">&nbsp;</td>
      </tr>
      <tr>
        <td width="23%" valign="top"><strong>Pathway 1:</strong> No action - See again</td>
        <td width="6%" align="center" valign="top"><input type="radio" name="allowed" value="0" id="allowed_0" /></td>
        <td colspan="2"><div class="required">
            <textarea name="comment[]" id="comment_1" cols="35" rows="5"></textarea>
          </div></td>
        <td width="33%" align="left" valign="top" class="p1_action"></td>
      </tr>
      <tr>
        <td valign="top"><strong>Pathway 2:</strong> See again</td>
        <td align="center" valign="top"><input type="radio" name="allowed" value="1" id="allowed_1" /></td>
        <td colspan="2"><div class="required">
            <textarea name="comment[]" id="comment_2" cols="35" rows="5"></textarea>
          </div></td>
        <td width="33%" align="left" valign="top" class="p2_action"></td>
      </tr>
      <tr>
        <td valign="top"><strong>Pathway 3:</strong> Refer to ED</td>
        <td align="center" valign="top"><input type="radio" name="allowed" value="2" id="allowed_2" /></td>
        <td colspan="2"><div class="required">
            <textarea name="comment[]" id="comment_3" cols="35" rows="5"></textarea>
          </div></td>
        <td width="33%" align="left" valign="top" class="p3_action"></td>
      </tr>
      <tr>
        <td valign="top"><strong>Pathway 4:</strong> Refer to Specialist</td>
        <td align="center" valign="top"><input type="radio" name="allowed" value="3" id="allowed_3" /></td>
        <td colspan="2"><div class="required">
            <textarea name="comment[]" id="comment_4" cols="35" rows="5"></textarea>
          </div></td>
        <td width="33%" align="left" valign="top"  class="p4_action"></td>
      </tr>
      <tr>
        <td valign="top">&nbsp;</td>
        <td align="center" valign="top">&nbsp;</td>
        <td colspan="2"><input type="submit" name="submit" id="submit" value="Create Pathways for Level 1" /></td>
        <td>&nbsp;</td>
      </tr>

    </table>
  </form>

浏览完代码后,发现所有四个文本框均已打开,并且用户将文本插入这四个框中的任何一个。

要解决此问题,请执行以下操作:

if ( strlen ( trim ( $comment ) ) > 0 )
{
    //use insert query
}

另一种方法是在提供复选框之前,以便使用者可以选择发送邮件的选项。

我已经通过使用解决了这个问题

$pathway_allowed = intval($_POST['allowed']);
$action = mysql_real_escape_string($_POST['actions']);


if(isset($_POST['submit'])){ 

$inserted_ids = array();
$pathway_comment = array();

foreach($_POST['comment'] as $comment) {
    $pathway_comment[]= mysql_real_escape_string($comment);
}

for($i=0, $count = count($pathway_comment);$i<$count;$i++) {
    $comment = $pathway_comment[$i];
    $query = sprintf(
        "INSERT INTO pathway (             
           pathway_pk,
           case_fk,
           level,
           pathway_allowed,
           comment
        ) VALUES (
           '',
           '$case_pk',
           '1',
           '%s',
           '$comment')", $pathway_allowed === $i ? 'y' : 'n');

$result = mysql_query($query, $connection) or die(mysql_error());
if ($result) {
$inserted_ids[] = mysql_insert_id();     
 }
}
$query_update = "UPDATE pathway SET pathway_action_fk = '$action' WHERE pathway_pk IN (" . implode(",", $inserted_ids) . ") AND pathway_allowed = 'y'";
$result_update = mysql_query($query_update, $connection) or die(mysql_error());
if($result_update){
$message = "- The pathways for level 1 have been created.";
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM