每当我按下这些 post 按钮中的任何一个时,只会更新列表中的第一个按钮,而不是我按下的特定按钮

Whenever i pressed any of those post buttons, only the first on the list will be updated instead of the speacific button that i pressed

<?php

 $sql = mysql_query("SELECT * FROM testimonial ORDER BY id DESC");
 //This block grabs the whole list for viewing
 while($row = mysql_fetch_array($sql)){
         $id = $row['id'];
         $name = $row['name'];
         $comment = $row['comment'];
         $visibility = $row['visibility'];
         $date_added = strftime("%b %d, %Y", strtotime($row['date_added']));}

 This section is for buttons if i press the button named update it should update the field named "visibility" to approved and if i press the button named update1 it should update the field named "visibility" to hide

 if(isset($_POST['update']))
    {
        $a =  addslashes(strip_tags($_POST['update']));
        $selected = mysql_query("SELECT * FROM `testimonial` WHERE id = '$id'");
        $info = mysql_fetch_assoc($selected);   
        $sql = mysql_query("UPDATE `testimonial` SET `visibility` = 'approved' WHERE id = '$id'");
    }
 if(isset($_POST['update1']))
    {
        $a =  addslashes(strip_tags($_POST['update1']));
        $selected = mysql_query("SELECT * FROM `testimonial` WHERE id = '$id'");
        $info = mysql_fetch_assoc($selected);       
        $sql = mysql_query("UPDATE `testimonial` SET `visibility` = 'hide' WHERE name='$name'");
    }

此行用于我开始循环的 table 的 header

    $sql = mysql_query("SELECT * FROM testimonial");
    echo '<form onSubmit="return validateForm()" action="testimonials.php" enctype="multipart/form-data" method="post">

        <div align="center">
        <table width="1273" border="1" cellspacing="0" cellpadding="6">
          <tr>
            <th width="119" bgcolor="#333333" scope="col"><strong>ID</strong></th>
            <th width="88" bgcolor="#333333" scope="col"><strong>User</strong></th>
            <th width="629" bgcolor="#333333" scope="col"><div align="center"><strong>Content</strong></div></th>
            <th width="107" bgcolor="#333333" scope="col"><strong>Status</strong></th>
            <th width="111" bgcolor="#333333" scope="col"><strong>Date Added</strong></th>
            <th width="111" bgcolor="#333333" scope="col"><strong>Action</strong></th>
            <th width="111" bgcolor="#333333" scope="col"><strong>Action</strong></th>
          </tr>';

此条件是更新 table 字段名称 "visibility" 以发布,如果可见性等于已批准,并且如果按下另一个按钮,则应更新 table 字段 "visibility" 不发布

    while($row = mysql_fetch_array($sql)){

        echo'<tr>';
        echo'<th width="119" scope="col">'.$row['id'].'</th>';
        echo'<th width="88" scope="col">'.$row['name'].'</th>';
        echo'<th width="629" scope="col"><div align="center">'.$row['comment'].'</div></th>';
        echo'<th width="107" scope="col">'.$row['visibility'].'</th>';
        echo'<th width="111" scope="col">'.$row['date_added'].'</th>';

        $activ= $row['visibility'];
    if ($activ=='approved'){ $visibility="Posted";} if ($activ=='hide'){$visibility="Not Posted";}
        echo '<th width="107" scope="col">'.$visibility.'</th>';
        echo'<th width="111" scope="col"><button type="submit" name="update" class="button" value='.$row['id'].'>Post</button><button type="submit" name="update1" class="button" value='.$row['id'].'>Unpost</button></th>';

        echo'</tr>';

    }

 ?>

第一个代码块中的 while 循环有问题。您正在尝试将 ID 存储在变量 id 中。但是您在每次迭代时都会覆盖 id 的值。

假设,您有 10 条 ID 为 1 2 3 ... 10

的记录

在第一次迭代中,您将值 10(因为 SQL 为 DESC)存储在 id

在第二次迭代中,您将用 9 覆盖该值。

所以在迭代结束时,您的 id 将只包含 1

id 的值为 1 是为 UPDATE 查询提供的,这就是为什么每次您的第一个 row/record 都会更新。

我对你的逻辑有点困惑。

试试这个:

由于您将 ID 作为每个 submit 按钮的值,在提交表单时,尝试获取提交按钮的值,如 $my_id = $_POST['update']

在您的 UPDATE 查询中使用此 $my_id