为什么编辑按钮仅对表php MySQL中的第1行起作用

时间:2018-10-11 22:19:03

标签: php mysql

为什么我的编辑按钮仅对表中的第1行起作用?

当我单击其他行时,它仍显示第1行的数据,但是id已更改。 当我更新1行时,数据将在数据库中更新。

//fetch the record to be updated
if (isset($_GET['edit'])){
    $entry_id = $_GET['edit'];
    $edit_state = true;
    $rec = mysqli_query($db, "select r.room_id, r.room_name, s.time_date, s.entry_id, s.time_exam, s.course_code, s.course_enroll from room r, schedule_entry s where s.room_id = r.room_id");
    $record = mysqli_fetch_array($rec);
    $time_date = $record['time_date'];
    $time_exam = $record['time_exam'];
    $course_code = $record['course_code'];
    $course_enroll = $record['course_enroll'];
    $room_name = $record['room_name'];
    $room_id= $record['room_id'];
    $entry_id= $record['entry_id'];
}

?> 

    <?php 
        if (mysqli_num_rows($results)>0){
        while ($row = mysqli_fetch_array($results)) { ?>
            <tr>
                <td width="180"><?php echo $row['time_date']; ?></td>
                <td width="70"><?php echo $row['time_exam']; ?></td>
                <td width="200"><?php echo $row['course_code']; ?></td>
                <td width="70"><?php echo $row['course_enroll']; ?></td>
                <td><?php echo $row['room_name']; ?></td>

                <td width="70">
                    <a class="edit_btn" href="entry.php?edit=<?php echo $row['entry_id']; ?>">Edit</a>
                </td>
                <td width="70">
                    <a class="del_btn" href="entryserver.php?del=<?php echo $row['entry_id']; ?>">Delete</a>
                </td>

            </tr>
        <?php } }?>  

2 个答案:

答案 0 :(得分:1)

您必须评估自己的语法,我认为$row['entry_id']具有相同的值,因此您必须var_dump($row)形式知道全部,

.... SOME CODE ....
    <?php 
        if (mysqli_num_rows($results)>0){
        while ($row = mysqli_fetch_array($results)) { ?>
            <tr>
                <td><? var_dump($row); ?>
            </tr>
        <?php } }?>  

然后我认为您可以解决您的问题...

答案 1 :(得分:0)

you need to run the loop to update the ids,

<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
    <td> <a class="edit_btn" href="entry.php?edit= 
<?php echo $row['entry_id']; ?>">Edit</a></td>
<?php
  }
} 
?>