简体   繁体   中英

Updating table using PHP, HTML and MySQL

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

     $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();

         }
     }

您似乎尚未从您删除的代码中向数据库发送任何数据

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