[英]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"> </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"> </td>
<td align="center" valign="top"> </td>
<td colspan="2"><input type="submit" name="submit" id="submit" value="Create Pathways for Level 1" /></td>
<td> </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.