如何在数据库中显示多行到多个文本框?

时间:2018-03-06 03:14:49

标签: php sql sql-server database

数据库中'lcAmount'中的数据为4行'15',我希望每行调用多个文本框。

数据库:

enter image description here

HTML CODE:

<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'];
  }

2 个答案:

答案 0 :(得分:0)

答案

$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'];

答案 1 :(得分:0)

这将显示多个文本框的多个值,您只需要在循环中获取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 
 ?>