dongxun5349 2019-05-05 04:37
浏览 99

我已批准每行上的按钮,点击它后,它应该更新该数据库中特定行的批准状态

I am displaying a table and each row of table have a approve and reject button and after clicking on approve the status should reflect to database as approved.

I tried for it but after clicking on any row's approve button it will set status as approved for all the rows.

My PHP code is:

$doc_re = "select * from files where receiver='$user'";
$record = mysqli_query($conn,$doc_re);

if($record)
{
    echo " <br><center><span class='badge badge-light' style='font-size:30px; background-color:teal;color:white;padding:10px;'>RECEIVED RECORDS</span></center>";
    echo "<br><table class='table' border='3' style='background-color:rgba(2,2,2,0.7);'>";
    echo "<thead class='thead-dark' >";
    echo "<tr style='font-size:23px;'><th style='color:skyblue;'><center>TO</center></th>
          <th style='color:skyblue;'><center>FROM</center></th>
          <th style='color:skyblue;'><center>FILE</center></th>
          <th style='color:skyblue;'><center>MESSAGE</center></th>
          <th style='color:skyblue;'><center>APPROVE</center></th>
          <th style='color:skyblue;'><center>REJECT</center></th>
          <th style='color:skyblue;'><center>STATUS</center></th></tr></thead>";

    while($r = mysqli_fetch_array($record,MYSQLI_ASSOC))
    {
        echo "<tr><td style='color:white; font-size:18px;'><center><strong>{$r['receiver']}</strong></center></td>
            <td style='color:white; font-size:18px;'><center><strong>{$r['sender']}</strong></center></td>
            <td style='color:white; font-size:18px;'><center><strong><a href='".$r['file']."'>{$r['file']}</a></strong></center></td>
            <td style='color:white; font-size:18px;'><center><strong>{$r['message']}</strong></center></td>
            <form method='POST'>
            <td><center>
            <button class='button button1' name='approve'  type='submit'><span>&#10003;</span></button></center></td>
            <td><center><button class='button button1' style='background-color:red;' name='reject' type='submit'><span>&#10008;</span></button></center></td>
            </form>
            <td style='color:white; font-size:18px;'><center><strong>{$r['Status']}</strong></center></td>
        </tr>";

        if(isset($_POST['approve']))
        {   
            $q1 = "update files set Status='APPROVED' where receiver='{$r['receiver']}' and sender='{$r['sender']}' ";
            $res = mysqli_query($conn,$q1);
        }
        elseif(isset($_POST['reject']) )
        {
            $q2 = "update files set Status='REJECTED' where receiver='{$r['receiver']}' and sender='{$r['sender']}' ";
            $ress = mysqli_query($conn,$q2);
        }   

    }
    echo "</table>";
} 

It set's status as approved for all the rows

  • 写回答

2条回答 默认 最新

  • dongyue3795 2019-05-05 05:25
    关注

    The row which you have selected might be having id, so you can used this id to passed to your query and set your status to approve or rejected like below :

     <td><center>
            <button class='button button1' name='approve'  type='submit'><a href="yourphp.page?status=approved&id=<?php echo $r['id'];?>"><span>&#10003;</span></a></button></center></td>
                    <td><center><button class='button button1' style='background-color:red;' name='reject' type='submit'><a href="yourphp.page?status=reject&id=<?php echo $r['id'];?>"><span>&#10008;</span></a></button></center></td>
    

    In above code , yourphp.page?status=reject&id=<?php echo $r['id'];?> this is important line ,here you are passing status & id of the row selected to some yourphp.page,also here i have assumed that $r['id'] this might be id of your table. Now to get value passed you can write like below :

    $status=$_GET['status'];//will give you status
    $id=$_GET['id'];//will give you id of row selected
    

    Passed above value in your query ,like below :

     $q1 = "update files set Status='$status' where id=$id";
                $res = mysqli_query($conn,$q1);
    

    Above query will update that particular row with status .And then after query is executed ,you can redirect to same page ..where your table is there .Hope this helps!

    Note : Also try using prepared statement it is safe an secure.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作