0

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>
2
  • Where on your form are you saying which id to update? Commented Jul 15, 2013 at 14:52
  • This is why you need to separate presentation from processing. And append LIMIT 1; to your update. And POST processing is not done inline but on top of the script, before any output so you get a chance to redirect. Commented Jul 15, 2013 at 14:53

1 Answer 1

2

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, e.g.

<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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.