我正在尝试从表中的每一行获取ID,以便能够在不同页面上的表单中更新每条记录。但是,我一直在尝试从表中获得第一条记录。有人可以帮忙吗。

<div class="personnel_title">Personnel<br><hr>
    <div class="addcontain">
        <form method="post" action="">
            <a href="HR_core_addpersonnel.php"><input type="button" name="addpersonnel" value="Add"></a>
        </form>

        <div class="searchcontain">
            <form method="post" action="HR_core_personnel.php">
            <input type="text" id="search" name="search" placeholder="Search" /><br>
            </form>
            <div id="output">

            </div>
        </div>
    </div>
    <hr>

    <?php
     $query = "SELECT `fighterID`, `firstName`, `middleName`, `lastName`, `rank`.`rank`, `status`, `telephone1`, `telephone2`, `stationlocation`.`exactlocation`, `hireDate`, `workShift`
              FROM `firefighterinfo`
              JOIN `rank` ON `firefighterinfo`.`Rank_rankID` = `rank`.`rankID`
              JOIN `stationlocation` ON `firefighterinfo`.`StationLocation_locationID` = `stationlocation`.`locationID`";
    $result = mysql_query($query);
    $num = mysql_num_rows($result);
    mysql_close();
    ?>

    <div class="results_table">
        <table>
            <thead>
                <tr>
                    <th>Firefighter</th>
                    <th>Rank</th>
                    <th>Status</th>
                    <th>Home No.</th>
                    <th>Cell No.</th>
                    <th>Station</th>
                    <th>Hire Date</th>
                    <th>Shift</th>
                    <th></th>
                </tr>
            </thead>
            <?php
            $i = 0;
            while($i < $num){
                $f1 = mysql_result($result, $i, 'firstName');
                $f2 = mysql_result($result, $i, 'middleName');
                $f3 = mysql_result($result, $i, 'lastName');
                $f4 = mysql_result($result, $i, 'rank');
                $f5 = mysql_result($result, $i, 'status');
                $f6 = mysql_result($result, $i, 'telephone1');
                $f7 = mysql_result($result, $i, 'telephone2');
                $f8 = mysql_result($result, $i, 'exactlocation');
                $f9 = mysql_result($result, $i, 'hireDate');
                $f10 = mysql_result($result, $i, 'workShift');

             ?>
            <tbody id="table_body">
            <tr>
                <td>
                    <?php echo $f1; ?>
                    <?php echo $f2; ?>
                    <?php echo $f3; ?>
                </td>
                <td>
                    <?php echo $f4; ?>
                </td>
                <td>
                    <?php echo $f5; ?>
                </td>
                <td>
                    <?php echo $f6; ?>
                </td>
                <td>
                    <?php echo $f7; ?>
                </td>
                <td>
                    <?php echo $f8; ?>
                </td>
                <td>
                    <?php echo $f9; ?>
                </td>
                <td>
                    <?php echo $f10; ?>
                </td>
                <td>
                    <a href="HR_core_updatepersonnel.php>"><img src="images/Awicons-Vista-Artistic-Edit.ico" width="15" height="20" alt="Edit"></a>
                </td>
            </tr>
            </tbody>
            <?php
                $i++;
                }
            ?>
        </table>
    </div>
</div>

最佳答案

@Farman给您一个很好的答案,这是我的一些补充


为我的眼睛重新格式化
将那些对mysql_result的丑陋调用更改为性能更好的东西
假定fighterID为右全键


这是你的剧本

<div class="personnel_title">Personnel<br><hr>
  <div class="addcontain">
    <form method="post" action="">
      <a href="HR_core_addpersonnel.php"><input type="button" name="addpersonnel" value="Add"></a>
    </form>
    <div class="searchcontain">
      <form method="post" action="HR_core_personnel.php">
        <input type="text" id="search" name="search" placeholder="Search" /><br>
      </form>
      <div id="output"></div>
    </div>
  </div>
  <hr>

<?php
 $query = "SELECT `fighterID`, `firstName`, `middleName`, `lastName`,
                  `rank`.`rank`, `status`, `telephone1`, `telephone2`,
                  `stationlocation`.`exactlocation`, `hireDate`, `workShift`
          FROM `firefighterinfo`
          JOIN `rank` ON `firefighterinfo`.`Rank_rankID` = `rank`.`rankID`
          JOIN `stationlocation` ON `firefighterinfo`.`StationLocation_locationID` = `stationlocation`.`locationID`";
$result = mysql_query($query);
$num = mysql_num_rows($result);
mysql_close();
?>
  <div class="results_table">
    <table>
      <thead>
        <tr>
          <th>Firefighter</th>
          <th>Rank</th>
          <th>Status</th>
          <th>Home No.</th>
          <th>Cell No.</th>
          <th>Station</th>
          <th>Hire Date</th>
          <th>Shift</th>
          <th>&nbsp;</th>
        </tr>
      </thead>
<?php
$c = array('fighterID','firstName','middleName','lastName','rank','status','telephone1','telephone2','exactlocation','hireDate','workShift');
while($r = mysql_fetch_assoc($result)) { ?>
      <tbody id="table_body">
        <tr>
          <td><?php echo $r[$c[1]].' '.$r[$c[2]].' '.$r[$c[3]]; ?></td>
          <td><?php echo $r[$c[4]]; ?></td>
          <td><?php echo $r[$c[5]]; ?></td>
          <td><?php echo $r[$c[6]]; ?></td>
          <td><?php echo $r[$c[7]]; ?></td>
          <td><?php echo $r[$c[8]]; ?></td>
          <td><?php echo $r[$c[9]]; ?></td>
          <td><?php echo $r[$c[10]]; ?></td>
          <td>
            <!-- the change made by @Farman Ullah is here -->
            <a href="HR_core_updatepersonnel.php?fighterID=<?php echo $r[$c[0]]; ?>">
              <img src="images/Awicons-Vista-Artistic-Edit.ico" width="15" height="20" alt="Edit">
            </a>
          </td>
        </tr>
      </tbody>
<?php } ?>
    </table>
  </div>
</div>


随时问任何问题或添加反馈

关于php - 试图从表行获取ID以执行更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20290340/

10-09 15:21