我正在学习PHP(并且非常有趣)但是我遇到了CRUD应用程序中的一个问题,我似乎无法找到解决方法。
我需要在创建页面上使用下拉列表来输入数据。我还希望在更新页面上有相同的下拉列表,但当然这需要将值设置为加载更新表单时已存储在记录中的值。我手动编写了一些较短的下拉列表(见下文),但我接下来需要做的是2008年的记录! (它将在键入输入时进行搜索。)
我查看了有关数组等的多篇帖子,我可以通过下拉列表来列出这些值,但无法弄清楚如何提取现有值。
相关代码如下(请对编码温和......我是新的!!):
<?php
require_once "../includes/header.php";
?>
<?php
if ( isset($_POST['patient_id']) && isset($_POST['datadate'])
&& isset($_POST['bedspace']) && isset($_POST['episode_id']) ) {
$sql = "UPDATE tbl_users SET patient_id = :patient_id,
datadate = :datadate, bedspace = :bedspace, isadmitday = :isadmitday, antimicrobial = :antimicrobial
WHERE episode_id = :episode_id";
$stmt = $PDO->prepare($sql);
$stmt->execute(array(
':patient_id' => $_POST['patient_id'],
':datadate' => $_POST['datadate'],
':bedspace' => $_POST['bedspace'],
':episode_id' => $_POST['episode_id'],
':isadmitday' => $_POST['isadmitday'],
':antimicrobial' => $_POST['antimicrobial']));
$_SESSION['success'] = '<div class="alert alert-success" role="alert"><strong>Record successfully updated</strong></div>';
header( 'Location: index.php' ) ;
return;
}
$stmt = $PDO->prepare("SELECT * FROM tbl_users where episode_id = :xyz");
$stmt->execute(array(":xyz" => $_GET['episode_id']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ( $row === false ) {
$_SESSION['error'] = '<div class="alert alert-danger" role="alert"> <strong>Unable to proceed - bad episode_id value</strong></div';
header( 'episode_id: index.php' ) ;
return;
}
$n = htmlentities($row['patient_id']);
$e = htmlentities($row['datadate']);
$l = htmlentities($row['bedspace']);
$i = htmlentities($row['isadmitday']);
$am = htmlentities($row['antimicrobial']);
$episode_id = htmlentities($row['episode_id']);
?>
<div class="container">
<div class="row">
<div class="row">
<div class="col-md-12">
<h2>Edit episode record</h2><br>
</div> <!-- /col-md-12 -->
</div> <!-- /row -->
</div> <!-- /row -->
<div class="row">
<form name="edit_record" id="edit_record" method="POST" action="">
<div class="form-group">
<label for="patient_id" control-label>Patient ID</label>
<input type="text" class="form-control" id="patient_id" name="patient_id" value="<?= $n ?>">
</div> <!-- /form-group -->
<div class="form-group">
<label for="datadate" control-label>Date</label>
<input type="text" class="form-control" id="datadate" name="datadate" value="<?= $e ?>">
</div> <!-- /form-group -->
<div class="form-group">
<label for="bedspace" control-label>Bedspace</label>
<select class="form-control" required="required" id="bedspace" name="bedspace" value="<?= $l ?>">
<option></option>
<option value="Side Ward 1" <?php echo $l == 'Side Ward 1'?'selected':'';?>>Side Ward 1</option>
<option value="Bed 1" <?php echo $l == 'Bed 1'?'selected':'';?>>Bed 1</option>
<option value="Bed 2" <?php echo $l == 'Bed 2'?'selected':'';?>>Bed 2</option>
<option value="Bed 3" <?php echo $l == 'Bed 3'?'selected':'';?>>Bed 3</option>
<option value="Bed 4" <?php echo $l == 'Bed 4'?'selected':'';?>>Bed 4</option>
<option value="Bed 5" <?php echo $l == 'Bed 5'?'selected':'';?>>Bed 5</option>
<option value="Side Ward 2" <?php echo $l == 'Side Ward 2'?'selected':'';?>>Side Ward 2</option>
<option value="Side Ward 3" <?php echo $l == 'Side Ward 3'?'selected':'';?>>Side Ward 3</option>
</select>
</div> <!-- /form-group -->
答案 0 :(得分:0)
好了经过几天密集的研究后,我的工作就像我想要的那样,虽然我确信我的解决方案并不漂亮!下拉列表是一种名为“抗菌药物”的抗生素药物清单。我使用SELECT语句从数据库中获取下拉列表,并且我匹配从另一个表中提取的值,以设置编辑记录时下拉列表的值。
以下代码来自edit.php文件:
<?php
if (isset($_POST['episode_id']) ) {
$sql = "UPDATE tbl_users SET antimicrobial = :antimicrobial
WHERE episode_id = :episode_id";
$stmt=$PDO->prepare($sql);
$stmt->execute(array(
':antimicrobial' => $_POST['antimicrobial']));
$_SESSION['success'] = '<div class="alert alert-success" role="alert"><strong>Record successfully updated</strong></div>';
header( 'Location: index.php' ) ;
return;
}
$stmt = $PDO->prepare("SELECT * FROM tbl_users where episode_id = :xyz");
$stmt->execute(array(":xyz" => $_GET['episode_id']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ( $row === false ) {
$_SESSION['error'] = '<div class="alert alert-danger" role="alert"><strong>Unable to proceed - bad episode_id value</strong></div';
header( 'episode_id: index.php' ) ;
return;
}
$am = htmlentities($row['antimicrobial']);
$episode_id = htmlentities($row['episode_id']);
?>
<div class="form-group">
<label for="antimicrobial" control-label>Antimicrobial from database</label>
<?php
$sql_am = 'SELECT antimicrobial FROM tbl_antimicrobials';
$stmt_am = $PDO->query($sql_am);
?>
<select class="form-control" id="antimicrobial"></select>
<?php
while ($row = $stmt_am->fetch(PDO::FETCH_ASSOC))
{
$selected = ($row['antimicrobial'] == $am ) ? ' selected' : '';
echo "<option value='" . $row['antimicrobial'] . "'" . $selected . ">" . $row['antimicrobial'] . "</option>";
}
echo '</select>';
?>
</div> <!-- /form-group -->