1

I have a table in my database known as adminapprove which has various fields. I use php to select all the data and display it inside a table.

I want one of the columns to have 2 buttons (approve and deny). I used a form for this based on https://stackoverflow.com/a/40958782/10217754

// $data contains the result of sql query (select *)

while($row = $data->fetch_assoc())
{
 echo "
 // Each row has multiple fields along with this
 <tr><td>
  <form method=\"post\">
   <input type=\"submit\" value=\"approve\">
   <input type=\"submit\" value=\"deny\">
  </form>
 </td></tr>";

 if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['approve']))  {
   approve($conn, $row);
   unset($_POST['approve']);
 }

 if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['deny'])) {
   deny($conn, $row);
   unset($_POST['deny']);
 }

// code for closing table tag echo </table>
}

This is the php script after the form (inside the while loop)

  • What I am aiming for is that whenever deny/approve is selected for some particular row, only that data(row) should be changed based on the option selected.

  • What is happening currently is that no matter whichever row I select, the last row in the table is changed based on the option selected.

I am new to PHP and thought loops work similarly as they do in C++ - So I call functions inside the loop only and once the functions complete executing, I unset the variable so that the next row is unaffected. But this is clearly not the case(at least that's what I understood, Correct me if I am wrong). So what am I missing? How can I achieve this?

Thanks in Advance.

Parth Kapadia
  • 507
  • 6
  • 18
  • Your PHP is a mix of HTML and PHP, which will cause a syntax error. Is that your problem? How do you know which row to change? Your form doesn't contain any identifier – Dharman Jul 04 '21 at 14:54
  • Please provide a full [mcve]. I can't understand how the 3 pieces of code connect to each other – Dharman Jul 04 '21 at 14:56
  • 1
    I have a feeling that you are just confused about the nature of request-response. As you probably know PHP is executed on the server, where it generates an HTML page that is then sent to the client. The page may contain JavaScript or forms that can trigger another request to the PHP server and pass some data along with it. In your case, you have a form that passes 0 information, so whatever script will handle it on PHP side, it will do absolutely nothing. I don't know what you are asking in regards to loops, though. – Dharman Jul 04 '21 at 14:59
  • 1
    I have provided an answer. It will explain to you how to identify a row in a HTML form. However, you must separate the logic on the server side. One code will produce the output, and another will handle the form input. – Dharman Jul 04 '21 at 15:05

0 Answers0