如何将数据库中的多行显示到多个文本框?
Posted
技术标签:
【中文标题】如何将数据库中的多行显示到多个文本框?【英文标题】:How to display multiple rows from database to multiple textbox? 【发布时间】:2018-08-13 20:06:09 【问题描述】:数据库中“lcAmount”中的数据为 4 行的“15”,我想将其调用到每行多个文本框。
数据库:
html 代码:
<div class="form-group">
<label class="control-label col-lg-4">Sick leave unconsumed</label>
<div class="col-lg-3">
<input type="text" id="sickLeave" name="sickLeave" class="form-control" <?php echo $sickUnconsumed; ?> />
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-4">Vacation uncomsumed</label>
<div class="col-lg-3">
<input type="text" id="vacation" name="vacation" class="form-control" <? php echo $vacationUnconsumed ?>/>
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-4">Maternity uncomsumed</label>
<div class="col-lg-3">
<input type="text" id="inpUN" name="inpUN" class="form-control" <? php echo $maternityUnconsumed ?>/>
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-4">Emergency uncomsumed</label>
<div class="col-lg-3">
<input type="text" id="inpUN" name="inpUN" class="form-control" <? php echo $emergencyUnconsumed ?>/>
</div>
</div>
控制器 PHP
$query = "SELECT lcAmount FROM leavecounts WHERE accountID = ?";
while($row = mysql_fetch_array($query))
$sickUnconsumed = $row['lcAmount'];
$vacationUnconsumed = $row['lcAmount'];
$maternityUnconsumed = $row['lcAmount'];
$emergencyUnconsumed = $row['lcAmount'];
【问题讨论】:
【参考方案1】:回答
$query = "SELECT lcAmount FROM leavecounts WHERE accountID = ? and ltypeID =1";
$params1 = array($accID);
$stmt = sqlsrv_query($con, $query, $params1);
while($row1 = sqlsrv_fetch_array($stmt))
$sickUnconsumed = $row1['lcAmount'];
$query1 = "SELECT lcAmount FROM leavecounts WHERE accountID = ? and ltypeID =2";
$params2 = array($accID);
$stmt1 = sqlsrv_query($con, $query1, $params2);
while($row2 = sqlsrv_fetch_array($stmt1))
$vacationUnconsumed = $row2['lcAmount'];
$query2 = "SELECT lcAmount FROM leavecounts WHERE accountID = ? and ltypeID =3";
$params3 = array($accID);
$stmt2 = sqlsrv_query($con, $query2, $params3);
while($row3 = sqlsrv_fetch_array($stmt2))
$maternityUnconsumed = $row3['lcAmount'];
$query3 = "SELECT lcAmount FROM leavecounts WHERE accountID = ? and ltypeID =4";
$params4 = array($accID);
$stmt3 = sqlsrv_query($con, $query3, $params4);
while($row4 = sqlsrv_fetch_array($stmt3))
$emergencyUnconsumed = $row4['lcAmount'];
【讨论】:
【参考方案2】:这将为多个文本框显示多个值,您只需在循环中获取 html或者您可以复制结果集返回的值并将它们存储在数组中并分隔 html。
<?php
$query = "SELECT lcAmount FROM leavecounts WHERE accountID = ?";
while($row = mysql_fetch_array($query))
$sickUnconsumed = $row['lcAmount'];
$vacationUnconsumed = $row['lcAmount'];
$maternityUnconsumed = $row['lcAmount'];
$emergencyUnconsumed = $row['lcAmount'];
?>
<div class="form-group">
<label class="control-label col-lg-4">Sick leave unconsumed</label>
<div class="col-lg-3">
<input type="text" id="sickLeave" name="sickLeave" class="form-control" value="<?php if(isset($sickUnconsumed)) echo $sickUnconsumed; ?>" >
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-4">Vacation uncomsumed</label>
<div class="col-lg-3">
<input type="text" id="vacation" name="vacation" class="form-control" value="<?php if(isset($vacationUnconsumed)) echo $vacationUnconsumed; ?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-4">Maternity uncomsumed</label>
<div class="col-lg-3">
<input type="text" id="inpUN" name="inpUN" class="form-control" value="<?php if(isset($maternityUnconsumed)) echo $maternityUnconsumed; ?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-4">Emergency uncomsumed</label>
<div class="col-lg-3">
<input type="text" id="inpUN" name="inpUN" class="form-control" value="<?php if(isset($emergencyUnconsumed)) echo $emergencyUnconsumed; ?>">
</div>
</div>
<?php
//while
?>
【讨论】:
以上是关于如何将数据库中的多行显示到多个文本框?的主要内容,如果未能解决你的问题,请参考以下文章