简体   繁体   中英

Update Single DB Record, Using PHP While Loop

I feel like there is an easy solution to this problem yet I can not figure it out. I am displaying the fields of my database using a while loop. At the end of each while loop is a form input where the admin can approve users. (All approved needs to do is change a 1 to a 2 in the approved column of that specific record).

All of the Approved records get changed instead of the specific one that I want. My question is how can I select and change only one record at a time. (The record in which gets approved). Thank you so much for any help you can provide me.

    <?php
      //select the database to write to
      $unapprovedTeachers = mysql_query("SELECT * FROM members_teachers WHERE approved = '1'", $dbc) or die("Could not select the unapproved teachers at this time.");
        //While loop to cycle through the rows
        while($row = mysql_fetch_assoc($unapprovedTeachers)){
            $teacher_id = $row['id'];
            echo $teacher_id;
    ?>
    <ul class="admin-fields">
    <?php
        foreach($row as $field){
            if(empty($field)){
                echo "....";
            }
            print '<li>' .$field.' </li>';
        }//End For Each Loop
        //print $teacher_id;


    ?>
            </ul>
    <footer>
     <?php
        if(isset($_POST['approve'])){
        mysql_query("UPDATE members_teachers SET approved = '2' WHERE id= ".$teacher_id."", $dbc) or die ("Oops something went wrong");
    }
      ?>
        <ul>
            <li>
                <form method="post">
                        <button name="approve" id="approve" type="submit">approve</button>
                </form>
               </li>
       </ul>
   </footer>
   <!--End New Row-->
  </section>
      <?php
    }//End While Lopp
    mysql_close($dbc);
       ?>
   </div>

You're simply outputting the SAME form for every row of data you display. You need to at least embed the ID of the relevant member inside each form, so the form can submit that specific ID, eg

<form method="post">
        <input type="hidden" name="teacher_id" value="$teacher_id" />
        <button name="approve" id="approve" type="submit">approve</button>
</form>

Note the hidden field. When the button gets submitted, you can then

$teacher_id = $_POST['teacher_id'];

to retrieve the ID, then issue the update query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM