0

I am trying to update a table in my database with PHP.

I used a loop to get all of the data from the table. Then, adding an "Approve" button in front of each row where the user can click if they want to approve the request, which would change the status to Approved.

Now I'm stuck, I can't find a way to update the Status field and I am not good with JavaScript.

I thought about making the button name dynamic, but I couldn't.

Output image

This is the loop code:

while($row = mysqli_fetch_array($result))
  {

echo "<tr bgcolor='#cccccc'>";
echo "<td>" . $row['nursename'] . "</td>";
echo "<td>" . $row['dr'] . "</td>";
echo "<td>" . $row['natureofreq'] . "</td>";
echo "<td>" . $row['clinic'] . "</td>";
echo "<td>" . $row['clinicfloor'] . "</td>";
echo "<td>" . $row['qitem'] . "</td>";
echo "<td>" . $row['moreinfo'] . "</td>";
echo "<td>" . $row['user_check'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td bgcolor='#009FE3'>" . $row['rstatus'] . "</td>";
echo '<form name="submit" method="post" action="">';
echo "<td>" . '<button id="approve" name="approve"> Approve</button>' . "</td>";
echo '</form>';
echo "</tr>";
}

This is the update code, which is not working:

$id=$row['id'];
if(isset($_POST['approve']))
{
  $allowed = mysqli_query($n," UPDATE newrequest SET rstatus = 'Approved' WHERE id = '$id' ");
}

I would appreciate any help.

output

4
  • Try $id = $_POST['id'];... if I understand what you’re trying to do, I guess you need to fetch whatever you send in the form... but $row is not set in the second code. Also, actually send it, because I think you are not. In other words, I’m assuming your update code is somewhere else because it should be... after all. Commented Nov 19, 2017 at 1:12
  • Can you please update your loop code with a row for database id? Commented Nov 19, 2017 at 1:20
  • Done, but what diff would it make? Commented Nov 19, 2017 at 1:38
  • Good. 'but what diff would it make?' - we will see later Commented Nov 19, 2017 at 1:41

2 Answers 2

2
     $url_of_current_page = $_SERVER['REQUEST_URI'];

     if(isset($url_of_current_page)){

         $_SESSION['url_of_current_page'] = $url_of_current_page;

     }

     $approve_row = '';

     while($row = mysqli_fetch_array($result)){

         $approve_row .= "<tr bgcolor='#cccccc'>

                             <td>" . $row['nursename'] . "</td>

                             <td>" . $row['dr'] . "</td>

                             <td>" . $row['natureofreq'] . "</td>

                             <td>" . $row['clinic'] . "</td>

                             <td>" . $row['clinicfloor'] . "</td>

                             <td>" . $row['qitem'] . "</td>

                             <td>" . $row['moreinfo'] . "</td>

                             <td>" . $row['user_check'] . "</td>

                             <td bgcolor='#009FE3'>" . $row['rstatus'] . "</td>

                             <td>
                                 <a href='action.php?id=" . $row['id'] . "'>
                                     <button>Approve</button>
                                 </a>
                             </td>

                         </tr>";

    }


     if(isset($approve_row)){

          echo $approve_row;

     }

And the action code. If your action page is not action.php, then tell me the name of the page.

     if(isset($_GET['id'])){

         $id = $_GET['id'];

         mysqli_query($n," UPDATE newrequest SET rstatus = 'Approved' WHERE id = '$id' ");

         if(isset($_SESSION['url_of_current_page'])){

              header( "Location: ". $_SESSION['url_of_current_page'] );
              exit();

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

2 Comments

It is working! thank you. Can you please give a breif explaination of what you did?
Fantastic. The 1st code save the name of the current page in a session variable so that it can be used at the action page. At the action page, the exit() is saying that return to the Approve buttons page after the update is done. Then on the while loop code $approve_row .=" "; returns all rows from the database table. But $approve_row must 1st be initialized(i.e $approve_row = ' ';). I used but link tags around the buttons because it is simpler that way and less coding.
0

You do not seem to be sending any data to the database yet from the code u dropped

1 Comment

No, there is data in the table but it is sent from a form in another code. The thing I am trying to do here is to change the Status to Approved with a button..

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.